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

  • Cosmo
    Anyone know of a printer with a 60" x 80" press?
    Cosmo
    We are looking for someone to print a job for us. Press size would need to be 60" x 80". Should be able to do 5 colors and multiple UV coatings.

    Anyone have any connections?
    Today, 05:37 PM
  • Obsidian86
    Reply to Happy Friday!!
    Obsidian86
    Don't forget to get plenty of sleep, eat a complete breakfast every morning and study. These are the best years of your life, they'll be over before you know it....
    Today, 05:15 PM
  • Kool
    Reply to Happy Friday!!
    Kool
    Hope you get feeling better soon Red.

    With the kids school starting next week I've been super busy with all the back to school rigmarole. It has been a very busy summer, now I understand...
    Today, 04:18 PM
  • PanToshi
    Reply to Happy Friday!!
    PanToshi
    Sorry to hear you're feeling poorly Miss Kittie. I hope you feel better soon! Thanks for being a major trooper and getting the QOTW post up despite power and sinus yuck problems. You're the Best! ...
    Today, 04:11 PM
  • PanToshi
    Reply to Who Owns the Copyright?
    PanToshi
    My dog did it.
    Today, 04:03 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