Announcement

Collapse
No announcement yet.

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

      Incredible Stock

      Latest Topics

      Collapse

      • SabrinaHartford
        Reply to Feedback on Hawaii dessert (baking) cook book cover
        SabrinaHartford
        The actual recipes are a mix of semi-Hawaiian and authentic Hawaiian (e.g. Koi, Haupia and Kulolo are proper Hawaiian Desserts, but then Manju Cake, Bibingka Cake, Chocolate Tofu Mousse pie and Hawaiian...
        Today, 02:42 AM
      • B
        Comment on [HELO] Convert web image for billboard
        B
        Well, who knows. They might have their reasons that would make sense if we knew them.

        You're right, though, it's their money and if that's what they insist they want -- despite your best...
        Yesterday, 09:21 PM
      • MazeMiami
        Reply to [HELO] Convert web image for billboard
        MazeMiami
        I'm grateful for the time both you spent to inform me and respect your expertise. Didn't mean to sound like I was disregarding what you shared. Just figure if it's not going to make much of a difference...
        Yesterday, 09:16 PM
      • B
        Reply to Get Your Graphic Design License Here
        B
        Hmmm, good question. Probably not. Most generally recognized professions seem to have licensing requirements, though, so I guess my comments stem from designers being somehow excluded and the profession...
        Yesterday, 09:14 PM
      • MazeMiami
        Reply to [HELO] Convert web image for billboard
        MazeMiami
        No one gave me a solution seems like either way I would get similar results. I suggested the clients use the file we already have in hand and they insisted they wanted vector. I'm open to having the imaged...
        Yesterday, 09:07 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