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

      • CobraStrike
        Reply to Help with a color palette
        CobraStrike
        Hello alber2410,

        So, you are probably going to get a million responses. Yellow or orange will go with both your purples and blues I am sure you know this. I would suggest an in-between...
        Today, 11:10 PM
      • CobraStrike
        Reply to Playing Cards
        CobraStrike
        Dear JofDiamonds,

        I like the work that you have produced. 1. Negative - While I notice the varying line width of the image it might not be enough to really make the work stand out. With that...
        Today, 10:50 PM
      • CobraStrike
        Critique Me...
        CobraStrike
        Hello World,

        I'm going to save you the sad story cliche of why I didn't make it into Graphic Design School four short years in the past. I went and decided to just get my BA in Studio Art....
        Today, 10:37 PM
      • CobraStrike
        Reply to My second work. Need your Remarks/Advices !
        CobraStrike
        OOOOO I had a great idea how you can show tension. You can put a big shark in a little jar that way the way to liberty is a struggle! but so gratifying once you reached it. Of course this will take...
        Today, 10:29 PM
      • Myron
        Reply to detailed print on truckers caps?
        Myron
        ...so cutting someone with your tongue is easy, but providing an example doc of your own is hard?
        Today, 08: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