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

      • Caleson
        Reply to Questions about Business Cards, Self Promotion
        Caleson
        Actually, I was referring to applying for employment. I'm kind of mixed on whether or not it would be a good idea, but I think overall, if the card is pretty creative, it could serve as a memorable token....
        Yesterday, 09:59 PM
      • fredz0003
        Logo critique for my personal blog
        fredz0003
        Hi, I am new to this forum. A bit about myself I am a programmer, and trying to build a professional looking blog. I've done web dev before, but I've never considered myself the artistic type. I followed...
        Yesterday, 04:57 PM
      • B
        Reply to Any Tech Geeks please help me?!
        B
        Of the options you mentioned, upgrading the RAM will make the most difference for the cost. Eight megabytes of RAM is almost a bare minimum when working with Adobe's graphics applications -- 16 is even...
        Yesterday, 04:56 PM
      • mrturner
        Any Tech Geeks please help me?!
        mrturner
        I am currently doing my graphics design work on my laptop which specs are listed below, It really does struggle to handle software like photoshop and illustrator. Do I, upgrade RAM? An 8GB ram stick for...
        Yesterday, 04:16 PM
      • PrintDriver
        Reply to RGB/CMYK/HEX to Pantone Reference for Print and Solid Chip
        PrintDriver
        You cannot match onscreen swatches of color matching systems to your on-screen unknown color. I just doesn't work that way.

        Though it is far from perfect, when I have to come up with the...
        Yesterday, 02: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