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

      • manishbjain
        Reply to Should I follow this Histogram ?
        manishbjain
        Kindly ignore the knob . I had already corrected it after I posted this. How abt otherwise ? Does the image look fine to you or the Before version was good. Do you feel the depth I felt required was actually...
        Today, 01:42 AM
      • KitchWitch
        Reply to Show customer Pantones used in design
        KitchWitch
        Hi Northeast Ink 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...
        Yesterday, 07:16 PM
      • Northeast Ink
        Show customer Pantones used in design
        Northeast Ink
        I am wondering if there is a setting or plug-in in Indesign or acrobat in which I can automatically place/show the Pantones I used in my design. Right now I have to draw out boxes and label the Pantones...
        Yesterday, 06:42 PM
      • praxis11
        Reply to NEW Simplified Logo for Travel Guide
        praxis11
        I think you'd do better to put a smily face in one of the circular letter forms, as that would at least emphasize commitment to 'nice'.
        Yesterday, 06:38 PM
      • PrintDriver
        Reply to Need Help with Creative Logo Design Advice
        PrintDriver
        If the logo is not what you had in mind, why do you show it here?
        If you are working with a crowdsource, let that be a lesson to you.
        Yesterday, 06:12 PM

      20% Off Sale at iStock until Sept 30, 2016
      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