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

  • rickinsa
    Comment on 7pt. Font size is industry standard for body text?
    rickinsa
    It was more than just putting up with a less knowledgeable person. She wanted to control everything and do it all her way. Much of what she said is so over the top wrong that it just baffles me and tried...
    Today, 08:44 AM
  • MichaelWied
    Reply to Some interesting Graphic Design debate discussions?
    MichaelWied
    Cool idea! I'm actually working on a blog myself. Just some advice that was given to me by someone who studies journalism is to have a years worth of content ready. So if it's a weekly blog, have no less...
    Today, 04:41 AM
  • garricks
    Reply to How to choose a scanner?
    garricks
    I bought a Canoscan based on PrintDriver's recommendation, and I'd buy another one in a heartbeat. It's a workhorse.
    Yesterday, 11:45 PM
  • PrintDriver
    Reply to How to choose a scanner?
    PrintDriver
    There aren't that many scanners to choose from any more. I use a Canoscan 8800 and I'd buy another Canoscan in a minute. Good machine.
    Yesterday, 11:39 PM
  • Buda
    Reply to Client hasn't paid and is using my website!!
    Buda
    I would never hand over the work to an untrustworthy client without getting payment. You let this happen.

    If you don't have a contract and deposit, take this as an expensive lesson to make...
    Yesterday, 09:35 PM
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

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