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

      • D&gr
        Reply to Spa website design, feedback please
        D&gr
        I'm not yet done coding, but you can visit it and see the design live here: khayoespa.com I spend hours coding that I don't think the design is any good. I need someone with years' of experience in website...
        Today, 10:51 PM
      • PrintDriver
        Reply to Empty PNG files?
        PrintDriver
        Are they something you are trying to right click?
        That doesn't always work. There are web tricks out there that make doing that give you a blank file.

        Asking permission to use the imagery...
        Today, 10:29 AM
      • docrocks
        Reply to Empty PNG files?
        docrocks
        Thanks PrintDriver. These are images downloaded from Ancestry.com and show census forms etc. I'll contact them and see if they can help.
        Today, 10:14 AM
      • PrintDriver
        Reply to Please Critique my Space Inspired Magazine
        PrintDriver
        Let me give you a little advice on Pantone's color of the year...

        They are always Textile swatches for one thing, when they should be standard design swatches. Most print houses don't carry...
        Today, 09:31 AM
      • PrintDriver
        Reply to Monitor for postproduction
        PrintDriver
        Monitors are sort of a matter of opinion.
        I do all my pre-press work on not so expensive Viewsonic monitors in a dual monitor setup. I already know that for print, my monitor will not accurately...
        Today, 09:13 AM
      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