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

      • bigdata
        Reply to What logo is this?
        bigdata
        Orville Redenbacher prelim?
        Today, 09:33 PM
      • indigomeadows
        Is there a software that will do this?
        indigomeadows
        I am trying to find a software that will do the following things, I don't need an expensive program, I don't want to buy an expensive program. I am a newb but I'm not an idiot but man, some of the programs...
        Today, 08:12 PM
      • seamas
        Reply to Dealing with the office power trip.
        seamas
        One thing about guy culture and women culture I have noticed is men will insult each other freely--especially if they are friends.
        Men often use ribbing and the like to feel each other out. If...
        Today, 05:00 PM
      • jp_isit
        Reply to Printing in 4C vs 2C - HELP!
        jp_isit
        Ok, thanks. Yes, this is their conversion... we're a bit rushed prioro to CNY. The good news is this is a proof so it's good to know duotone is a better option if it doesn't come out ok and we need...
        Today, 04:53 PM
      • John Ruggieri
        Reply to Font ID please - Trajan Pro??
        John Ruggieri
        All set! Client came through. It's Meta.
        Today, 04:25 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