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

  • PrintDriver
    Reply to Norman Vintage Wedding Invite
    PrintDriver
    If you want email notification, you have to turn it on in your user preferences.
    Yesterday, 11:25 PM
  • PrintDriver
    Reply to Can't send attachments from Illustrator...
    PrintDriver
    When you saved your illustrator file, you need to save it with PDF enabled in order to drag and drop it.
    That said, you shouldn't be doing it this way.
    If it is just a proof for your client...
    Yesterday, 11:24 PM
  • B
    Reply to Newspaper Graphic Designer
    B
    Ad design and editorial design are usually handled out of separate departments at larger newspapers. Editorial design is often divided between "pagination" (laying out the news pages) and feature...
    Yesterday, 10:52 PM
  • EightyTwenty
    Reply to Need tips with designing a logo
    EightyTwenty
    Hey guys!

    So I have been pondering how I could make this work, I tried to follow a few youtube videos on branding and how to display the proper ways of your core identity.

    I...
    Yesterday, 09:25 PM
  • bubby17346
    Reply to Can't send attachments from Illustrator...
    bubby17346
    Can you save the files out of AI as a .pdf and send that to the client. That's how I send all of my files to clients and coworkers alike. I keep the .ai files on my computer but always save as a .pdf...
    Yesterday, 09:15 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