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

      • iwebsitez
        Reply to Tours Travel company logo, what do you think?
        iwebsitez
        I quite like it to be honest, it's not too stuffed but it's clean, easy to remember! Well done Dan.
        Today, 11:18 AM
      • Craig B
        Reply to Help me explaining my job in my portfolio!
        Craig B
        I think what you described is fairly typical of most jobs. I'm not saying it's identical but I do web design, email design, interactive apps, collateral, booth graphics, signage, posters, advertising,...
        Today, 10:46 AM
      • Colton819
        Reply to Getting a image ready for print
        Colton819
        So I tried doing Pantone colors but obviously they just couldn’t match close enough. The image I posted is actually a vector format I created a raster format then converted to vector in illustrator. I...
        Today, 08:47 AM
      • leaagg
        Help me explaining my job in my portfolio!
        leaagg
        Hello,

        I am a French designer working for a Turkish company (in turkey).
        I am now currently working on my updated portfolio because my main goal is QUIT !! The working conditions in...
        Today, 07:57 AM
      • PrintDriver
        Reply to Tours Travel company logo, what do you think?
        PrintDriver
        Do people still use QR codes?
        Two phones ago I downloaded an app just to check that a client's QR code was linking properly when printed. 4 years later now, and that app has never been used again....
        Today, 07:06 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