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 Problem with div help please?
    skribe
    @ vaprarts, I don't think what you are pointing out there is really going to help, because unless I am seriously mistaken, mikhende's site is running on WordPress.

    @mikhende, On a typical...
    Today, 08:34 AM
  • skribe
    Reply to Database software printing
    skribe
    Welcome Robert,

    As you are a new member please take a few minutes and read though our rules and guidelines for new members. The following links should get you up to speed.

    GDF...
    Today, 07:57 AM
  • Robert1975
    Database software printing
    Robert1975
    Hi guys,
    I'm working in card manufacturing industry, so i'm printing on A4 pvc sheets.
    I'm trying to find a software which can do the followings:
    - arrange on A4 format 10 layouts (2...
    Today, 06:59 AM
  • lowfatgraphics
    Reply to Break points and Responsive design
    lowfatgraphics
    Not for the client, we already have the project.

    This issue came up recently when one of our projects was in development. The developers were doing a poor job and I had a lot of back and...
    Today, 01:57 AM
  • garricks
    InDesign data merge adviceā€¦
    garricks
    I've been given the task of creating a directory of doctors, that will be redone twice a year. Tell me if you think this setup is insane, or if there's a better way.

    It's sectioned as fo...
    Today, 01:56 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

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