Announcement Announcement Module
Collapse
No announcement yet.
basic javascript question Page Title Module
Move Remove Collapse
Search Search Module
Collapse

Advertisement Advertisement Module
Collapse

Featured Images Featured Images Module
Collapse

Mediabistro Creative Sites Mediabistro Creative Sites Module
Collapse
Latest Topics Latest Topics Module
Collapse

  • MichaelWied
    Comment on Company Logo Rebranding Critique
    MichaelWied
    Cant' see it for some reason.
    Today, 04:15 AM
  • Buda
    Reply to Please help with company logo
    Buda
    Is the company name still The Fitness Doctors? The latest logo doesn't show this.
    Today, 03:38 AM
  • Buda
    Reply to Happy Last Friday of July!!!!
    Buda
    Oh lala, if your summer is ending, that means my winter is ending.

    Amazing pirate mermaid wedding! I went to a pirate birthday party on a boat once but that doesn't even compare to a wedding....
    Today, 03:35 AM
  • salsa
    Reply to Freelance Advice - Family Conflict
    salsa
    If it were me, I'd just write off all the work done so far as pro bono. Like several others already mentioned, I do all family work for free and if they want to pay me then that's an extra bonus.
    ...
    Today, 03:23 AM
  • Kayekaye
    Reply to Freelance Advice - Family Conflict
    Kayekaye
    I had inlaws approach me for a logo and website. What a nightmare. They are a married professional couple and said "Of course we would pay you, $___ (an amount that barely covers 2 hours of work)...
    Today, 03:07 AM
Advertisement Advertisement Module
Collapse

Sponsors Sponsors Module
Collapse

X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • basic javascript question

    how do i make this function display properly?
    The alert do not add the numbers, instead they treat it as strings i guess and alerts "The total value is 5711" when i want it to say "The total value is 23"

    <html>
    <body>

    <script type="text/javascript">

    /* Avoiding errors using typeof operator */

    function addThree(arg1, arg2, arg3)
    {

    if ( (typeof arg1 !="number") || (typeof arg2 !="number") || (typeof arg3 !="number") )
    alert("Please input only numbers!");
    else
    alert("The total value is "+arg1+arg2+arg3);
    }

    var x=5, y=7;
    addThree(x, y , 11);


    </script>

    </body>
    </html>

  • #2
    If you use + on a number, it converts it to a string I think. + is used to concatenate strings, variables, etc.

    However, adding up nothing but integers works fine. What you are doing, is adding a string, then 3 numbers, which wont work.

    HTML Code:
    <script type="text/javascript">
    
    function addThree(arg1, arg2, arg3)
    {
    	if ( (typeof arg1 !="number") || (typeof arg2 !="number") || (typeof arg3 !="number") )
    	{
    		alert("Please input only numbers!");
    	}
    	else
    	{
    		total = arg1+arg2+arg3;
    		alert("The total value is " + total);
    	}
    }
    
    addThree(1,2,3);
    
    </script>

    Comment


    • #3
      I see, so you would have to set the total of the arguments in a variable first and then declare that variable with the string.

      thanx

      Comment

       
      home | site map | advertising/sponsorships | about us | careers | contact us | help courses | browse jobs | freelancers | events | forums | content | member benefits | reprints & permissions about | terms of use | privacy policy | Copyright © 2014 Mediabistro Inc. Mediabistro Inc. call (212) 389-2000 or email us
      Working...
      X