Announcement

Collapse
No announcement yet.

basic javascript question

Collapse
X
  • 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

      Search

      Collapse

      Sponsor

      Collapse

      Incredible Stock

      Latest Topics

      Collapse

      • bigdata
        SVG charting
        bigdata
        Bit slow to find this one but sure glad I did:

        highcharts dot com

        "Highcharts is a SVG-based, multi-platform charting library ... easy to add interactive, mobile-optimized...
        Today, 02:12 AM
      • B
        Comment on Assessment Questions help!
        B
        No problem. The forum software doesn't make it as clear as it should.
        Today, 12:24 AM
      • CheridaMachen
        Reply to Assessment Questions help!
        CheridaMachen
        Sorry im new here i thought there was an issue why it wasnt posting didnt realised it needed to be approved but thanks!
        Today, 12:15 AM
      • B
        Reply to Assessment Questions help!
        B
        Hi CheridaMachen. Welcome to the forum!

        The first few posts from every new forum member are moderated, so a moderator, like me, needs to approve them. Since there's not always a moderator...
        Yesterday, 11:50 PM
      • CheridaMachen
        Assessment Questions help!
        CheridaMachen
        Okay so im currently studying at Open colleges and just wondering if any Industry professionals could answer these questions?
        -When are current trends in the Graphic Design industry?
        -Have...
        Yesterday, 11:14 PM
      GDF A division of Mediabistro Holdings Adweek | Mediabistro | Clio | Film Expo Group Contact Us | Terms of Use | Privacy Policy Copyright 2016 Mediabistro Holdings
      Working...
      X