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

  • gspasic
    Reply to Website Feedback for Design Course
    gspasic
    Thanks for the feedback ash! You've made some very good points that I'll go back and work on.
    Today, 12:41 AM
  • PrintDriver
    Reply to Corel Draw
    PrintDriver
    What are you going to do with it?
    It's used a lot in the sign industry with in-house design for in-house printers,
    but has sort of fallen out of favor in (at least my end of) the print industry....
    Today, 12:02 AM
  • allie.ceee
    Can you ID this font?
    allie.ceee
    A friend of mine asked me to try to find the font used in the picture for their clothing line. I'm wondering if any of you have suggestions on similar fonts that I can show them! Thanks for your help,...
    Yesterday, 11:37 PM
  • Oliver Diaz
    Reply to A4 sheets. One side white + one side Pantone color?
    Oliver Diaz
    Thanks for your welcome message KitchWitch. I read the rules, it's all good!
    Yesterday, 11:07 PM
  • KitchWitch
    Reply to Hey Folks
    KitchWitch
    Hi Ace and welcome to GDF.

    We ask all new members to read very important links here and here. These explain the rules, how the forum runs and a few inside jokes. No, you haven't done anything...
    Yesterday, 10:41 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