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

  • creare
    Reply to Personal Logo Concepts
    creare
    oh..oh.. is a great problem , you need to look at the best logos that have been made, for example, look for works of important studies; three basic things, simplicity ', uniqueness', visible also in...
    Today, 07:13 PM
  • creare
    Reply to logo lettering new york
    creare
    Thanks to all, yes now i try to do something different , yes Cosmo it is true , which then is more hard to do everything with your computer without drawing first , in fact learn to draw is crucial is...
    Today, 06:58 PM
  • B
    Reply to Personal Logo Concepts
    B
    Cysir, you said this is a personal logo, yet you've used the plural of studio in the logo. Do you really have more than one studio?
    Today, 06:10 PM
  • kemingMatters
    Reply to Dojo Dragon Shirt Design.
    kemingMatters
    Not that I'm a pro on chinese dragons but I can't say I've ever noticed one that didn't have feet; usually they have talon like appendages often without a joint like a knee or elbow... I enjoy the colour...
    Today, 06:03 PM
  • Salt
    Comment on Dojo Dragon Shirt Design.
    Salt
    I was going for the chinese dragon look with no limbs on it. Also I wanted to keep it simple.
    Today, 05:55 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.
      Working...
      X