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

  • skribe
    Reply to Charter Schools Brochure
    skribe
    I like the overall aesthetic too. Along with the things commented on by the others, I also notice that numbering under "EFFC Acomplisments" does not align with the top of paragraphs, and seems...
    Today, 03:32 AM
  • skribe
    Reply to RGB or HEX values to Pantone?
    skribe
    As I mentioned in a post just recently I nearly always have the client get their hands on some pantone books and have them choose the colors themselves. If they are local they can look at mine, if they...
    Today, 03:23 AM
  • bahmaine
    Reply to Charter Schools Brochure
    bahmaine
    Thanks Buda. I don't know what text will go on the front. Unfortunately, I don't have a wide selection of photos to choose from so I chose one that was telling the end goal of charter school education....
    Today, 01:13 AM
  • PrintDriver
    Reply to RGB or HEX values to Pantone?
    PrintDriver
    This whole question is making my head explode.

    There are millions of RGB combinations. There are only 3000-and-change Pantone colors.
    What do you have as a reference for the RGB values....
    Today, 12:41 AM
  • MrVanHelsing
    Reply to Need help advancing my skill in design
    MrVanHelsing
    @KitchWitch thank you

    @Buda yes i realize that it's better to have a client with a specific style and message, but in my company that is hardly if ever the case. I tought that keywords...
    Today, 12:39 AM
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 © 2015 Mediabistro Inc.
      Working...
      X