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

Advertisement Advertisement Module
Collapse

Latest Topics Latest Topics Module
Collapse

  • designzombie
    Reply to Client contact methods
    designzombie
    It's taken me a while to get use to the fact that younger people prefer text, and that a voice call is like showing up on their door step unannounced. I feel fortunate to be old enough to know when voice...
    Today, 05:10 PM
  • B
    Reply to Client contact methods
    B
    I'm not a big fan of telephone calls with clients for all the reasons Paranoir mentioned. Even so, many people prefer calling, so I have a phone and I make adjustments to accommodate their preferences....
    Today, 05:06 PM
  • PrintDriver
    Reply to Random Numbers in InDesign
    PrintDriver
    I just found this with a quick web search:
    https://forums.adobe.com/thread/636110

    I didn't test it.
    Today, 04:38 PM
  • PrintDriver
    Reply to Client contact methods
    PrintDriver
    What everyone seems to be dancing around here is the fact that if you plan to deal with high end clients for 5 or 6 figure contracts, you better plan on answering the phone when a client calls....
    Today, 04:33 PM
  • Brie
    Reply to Dyslexic typeface
    Brie
    B, I can relate, I have what I've always referred to as "number-lexia". Same difficulty seeing things in the correct order. Sometimes, typing, I have "finger-lexia".
    Today, 02:54 PM

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 © 2015 Mediabistro Inc.
      Working...
      X