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

  • PrintDriver
    Reply to How to imitate this crackle design/texture for a T-shirt?
    PrintDriver
    If you are silkscreening this, taking it into Photoshop isn't a very good idea. When designing T-shirts for silk screening you have to keep the color separations in mind. One color is easy from Photoshop....
    Today, 01:48 PM
  • kemingMatters
    Reply to Unused designs - who owns them?
    kemingMatters
    IMHO selling your unused ideas is akin to a carpenter selling off scraps and sawdust to a pressboard manufacturer. Your unused ideas/concepts are byproducts of the project your client hired you to co...
    Today, 01:44 PM
  • hank_scorpio
    Reply to Working on a logo for myself
    hank_scorpio
    I don't particularly like any of those birds, it's not really the style I wanted.

    I agree the typography is out of place but the text is just a placeholder for now.

    I'll try...
    Today, 01:39 PM
  • KitchWitch
    Reply to Oh Portland!
    KitchWitch
    Hi Megoggsc 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:37 PM
  • KitchWitch
    Comment on Delete post from years ago
    KitchWitch
    In the Portland thread? A mod can remove the links for you. Do you want both of those links removed?
    Today, 01:36 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.
      Working...
      X