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

Advertisement Advertisement Module
Collapse

Latest Topics Latest Topics Module
Collapse

  • donwu212
    Logo Design for Neighborhood Council
    donwu212
    Hey everyone. This is my first post and hoping to get some feedback on two logo designs.

    I'm re-designing a logo for a neighborhood council organization called Brighton Park Neighborhood...
    Today, 12:40 AM
  • PrintDriver
    Reply to Donald Trump for President!
    PrintDriver
    The debates are going to be a sideshow. It's almost like the rest of the country doesn't exist right now except for NH and Iowa. None of them have addressed any really important issues. And I mean on...
    Today, 12:35 AM
  • PrintDriver
    Comment on Do I need to market myself ?
    PrintDriver
    Starting a business after being in the industry working for someone that knows what they are doing BEFORE freelancing makes more sense.
    Today, 12:32 AM
  • PrintDriver
    Reply to Templates- to use or not?
    PrintDriver
    Oh yeah. Found it. You promised your work was original work when you signed up.
    No Templates for You.

    The Freelancer really gets the sh!t end of the stick here.

    Did you...
    Today, 12:20 AM
  • PrintDriver
    Reply to Templates- to use or not?
    PrintDriver
    Your first mistake is the reverse auction website. You are never going to make a living from the likes of those things. Where you work now, do you do work for sites like that? If not, why would you as...
    Yesterday, 11:50 PM

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