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

      Advertisement

      Collapse

      Latest Topics

      Collapse

      • DanArt
        Reply to Struggling with Graphic Design Career
        DanArt
        For me I do illustration. So my advice is to find your likes and dislikes and focus on that. The problem I find is that people want me to do things I have no experience or knowledge about. That is when...
        Today, 01:54 PM
      • DanArt
        Reply to Hardware Recomendations
        DanArt
        There is absolutely nothing "compact" enough to do the work that is required. Unless you do not mind being restricted in print size and speed. When I get the funds I am going to do just that....
        Today, 01:45 PM
      • DanArt
        Reply to Wedding Invites
        DanArt
        A wedding invitation could be anything. You could be as creative as you want to be
        Today, 01:32 PM
      • B
        Reply to Classrom Ads Projects
        B
        You've really gotten lots of good feedback here, which is actually good, I think.

        Just an observation, in case it's seeming like everyone is piling on. I've noticed when things posted here...
        Today, 01:32 PM
      • DanArt
        Reply to Thou name is Dan and my apple have salt
        DanArt
        But is that what they do for a living 24/7 draw and illustrate ?

        I am trying to say that I am a illustrator first and then I am a designer second....
        Today, 01:15 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