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

      • PrintDriver
        Reply to Portfolio Question!
        PrintDriver
        If you are talking about a portfolio you have in a case and take with you to interviews where you will be doing a presentation of the work, a written blurb is not needed.

        If you are talking...
        Today, 07:04 PM
      • PrintDriver
        Reply to Souq White Friday Campaign
        PrintDriver
        I guess I'm sort of confused. If Friday is a day of religious observance, aren't you going to run into a little bit of pushback on this?

        Are you targeting the traditional type of Christmas...
        Today, 06:58 PM
      • PrintDriver
        Reply to question about powerpoint custom slide sizing
        PrintDriver
        Your ppi is dependent on your print method.
        Where are you going to get a 142cm (56") square image printed? And on what?
        What is your final output purpose? Fine art or a short term poster?...
        Today, 06:56 PM
      • PrintDriver
        Reply to Please critique my work
        PrintDriver
        As far as stock is concerned, what works and what doesn't is entirely up to the project at hand.
        Do I like any of these better than the others? Sure. But what gets used isn't up to what I like....
        Today, 06:51 PM
      • zurichluck
        Reply to question about powerpoint custom slide sizing
        zurichluck
        thanks KitchWitch, very much appreciate your help as always. yes i see!

        i would like to ask another question, which is at least 5 times less stupid than the last one. i am (eventually) going...
        Today, 06:50 PM

      Incredible Stock
      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