Announcement

Collapse

Critical Announcement

With great regret, we would like to inform you that the decision has been made to close down www.GraphicDesignForum.com as of January 31, 2018.

The community has started www.GraphicDesignForum.org to continue the discussion.
See more
See less

basic javascript question

Collapse
X
  • 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

      Search

      Collapse

      Sponsor

      Collapse

      Latest Topics

      Collapse

      • B
        Reply to What do you guys think about this designe ?
        B
        I'm guessing that renting a party girl means exactly what one might suspect.

        As for the logo, it looks like a peel-off sticker of some kind. The type is mathematically centered, but visually...
        Today, 09:07 PM
      • PrintDriver
        Reply to migrating or going away?
        PrintDriver
        Merit badges? Really?
        We'll see what happens I guess.
        Today, 08:19 PM
      • B
        Reply to migrating or going away?
        B
        The project manager, Iraszi, from the company that owns the forum, says the company decided to shut down the forum. He's taken the initiative to set up a new, independent forum that isn't owned by the...
        Today, 07:55 PM
      • PrintDriver
        Reply to What do you guys think about this designe ?
        PrintDriver
        What do you mean by ''gig design''?

        As for the thing itself, I might put Perth on its own line under Party Girls. The whole thing kind of gives a mixed message. Is it a modeling agency? Is...
        Today, 07:34 PM
      • PrintDriver
        migrating or going away?
        PrintDriver
        Is this forum migrating to the new or going away and starting over?
        Today, 07:30 PM
      GDF A division of Mediabistro Holdings Adweek | Mediabistro | Clio | Film Expo Group Contact Us | Terms of Use | Privacy Policy Copyright 2016 Mediabistro Holdings
      Working...
      X