Announcement Announcement Module
Collapse
No announcement yet.
basic javascript question Page Title Module
Move Remove Collapse
Search Search Module
Collapse

Advertisement Advertisement Module
Collapse

Featured Images Featured Images Module
Collapse

Mediabistro Creative Sites Mediabistro Creative Sites Module
Collapse
Latest Topics Latest Topics Module
Collapse

  • cdgc
    Retail vs Contract Pricing
    cdgc
    Looking for some input on this matter.

    Many years ago we published a map for railroad enthusiasts that was 1/1 black and folded several times into 3.5" by 9". Nothing all that...
    Today, 09:28 PM
  • Kayekaye
    Reply to Are you able to read the text?
    Kayekaye
    Better photo, but flip it so the eye travels left to right. Having on the left side it kind of stops the eye there.

    Have you not learned about line lengths? This is about 88 characters long....
    Today, 09:14 PM
  • sketxz!
    Reply to BEST COMPUTER CONFIGURATION FOR GRAPHIC DESIGNERS?
    sketxz!
    So I got the new SSD and it's amazing. It fits in the palm of my hand but so much speed compared to my old one. Here's some graphs of before and after (keep in mind it was a buggy 5 year old drive):...
    Today, 08:43 PM
  • <b>
    Reply to Opinions on My Logo?
    <b>
    It's an interesting-looking graphic, but for the reasons already mentioned, it doesn't really meet the usual requirements of a logo. I'm not a big fan of Lobster either, but that's just me.
    Today, 08:37 PM
  • <b>
    Comment on Too old to switch to the field of graphic art?
    <b>
    Well, I don't know. Increasingly a four-year degree is needed to get past HR hiring requirements in many companies.

    Your situation might be a little different, though. You mentioned a background...
    Today, 07:22 PM
Advertisement Advertisement Module
Collapse

Sponsors Sponsors Module
Collapse

X
Conversation Detail Module
Collapse
  • 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

      Mediabistro A division of Prometheus Global Media home | site map | advertising/sponsorships | careers | contact us | help courses | browse jobs | freelancers | content | member benefits | reprints & permissions terms of use | privacy policy Copyright © 2014 Mediabistro Inc. call (212) 389-2000 or email us
      Working...
      X