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

      • bahmaine
        Ilustrator Tracing and Typing Within a Path
        bahmaine
        HI All,

        I was trying to recreate the following image in Illustrator. I wanted to trace over the image and take out the text used for the neighborhoods but it's taking me a while. Can someone...
        Today, 02:47 PM
      • B
        Comment on My first piece for critiquing
        B
        It's especially the type with the ampersand behind it, but other issues contribute to the legibility problem throughout.

        You've used four different typefaces. It's not a hard-and-fast rule,...
        Today, 11:08 AM
      • PrintDriver
        Reply to How to make lace with illustrator?
        PrintDriver
        Most lace is a repeat pattern. Once you get the corner block and a running block done, it's all gravy from there... Getting it to work as a brush must take some skillz. I wouldn't want to try it, not...
        Today, 09:09 AM
      • PrintDriver
        Reply to InDesign, transparency, PDFs and Acrobat's Optimizer
        PrintDriver
        I'm not quite understanding why a 76mb 12 page doc would fail on your printer's end. That, to me anyway, is small for so many pages.
        I'd be more concerned about your boss being really mad for missing...
        Today, 09:05 AM
      • PrintDriver
        Reply to Feedback Please. Thank you.
        PrintDriver
        Posters?
        Or book covers?

        A poster generally communicates an idea and has a call to action. If these are posters to sell these two books, they are a fail.

        The illustrations...
        Today, 08:22 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