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

      • PrintDriver
        Reply to Digital Duplicator printers
        PrintDriver
        There's a lot of stuff online about using digital duplicators for thermo. I looked cuz I wondered what the post was about. Most of it sounds to me like a slapdash way of doing a supposedly high end type...
        Today, 07:12 PM
      • CourtneyKay
        My official introduction!
        CourtneyKay
        I realized I am rather new here and haven't made an official introduction!

        So without further ado, my name is Courtney and I am a graphic designer. I studied advertising in school, and currently...
        Today, 05:06 PM
      • Biggs097
        Reply to Digital Duplicator printers
        Biggs097
        You'll need a bit more than just a duplicator or offset press for thermography.
        And I'm afraid a digital machine wont be an option at all.

        Traditional thermo prints also get passed...
        Today, 02:52 PM
      • KitchWitch
        Reply to Digital Duplicator printers
        KitchWitch
        Hi irishdz and welcome to GDF.

        We ask all new members to read very important links here and here. These explain the rules, how the forum runs and a few inside jokes. No, you haven't done...
        Today, 01:00 PM
      • irishdz
        Digital Duplicator printers
        irishdz
        Recently I decided to include thermography in my small business but have struggled to find a good wet ink/ digital duplicator printer. Does anyone know of a good one?
        Today, 12:50 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