Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2003
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Variables and Document.Write

    <script language="Javascript">
    <!---
    var hours=prompt("How many hours did you work?", "enter hours");
    var pay=prompt("What level are you on?", "you can select from: trainee, level2 or level3");
    trainee="6.84";
    level2="7.47";
    level3="7.61";
    result=(hours*pay);
    document.write(result);
    -->
    </script>

    I was wondering why the result prints as NaN, what is stopping it from printing the amount?
    Help appreciated

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    if you input strings in the prompts (or you click Cancel), the result would be NaN

  • #3
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would recommend using a server-side language to do this since it would be just as easy to do. You need to remember that 13% of users don't have JavaScript-enabled browsers. However, if you still want to use JavaScript for this, this is how it should be written:
    Code:
    <script type="text/javascript">
    	var hours = prompt( "How many hours did you work?", "" ), pay = 0;
    	
    	switch( prompt( "What level are you on (trainee, level2 or level3)? ", "" ).toLowerCase() )
    	{
    		case "trainee": pay = 6.84; break;
    		case "level2": pay = 7.47; break;
    		case "level3": pay = 7.61; break;
    		default: alert( "Invalid level.\n" ); break;
    	}
    
    
    	document.writeln( "<strong>Pay:</strong> $" + ( ( ( hours ) ? hours : 0 )  * pay ) );
    </script>
    Last edited by fredmv; 10-24-2003 at 11:23 AM.
    Moderator @ WebDeveloper.com
    Mentor @ WebXpertz.net

  • #4
    New Coder
    Join Date
    Aug 2003
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks

    Now'll I'll try to figure out how it works. Thanks again!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •