...

View Full Version : Variables and Document.Write



surfie
10-24-2003, 08:19 AM
<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?:confused:
Help appreciated:D

glenngv
10-24-2003, 09:32 AM
if you input strings in the prompts (or you click Cancel), the result would be NaN

fredmv
10-24-2003, 10:27 AM
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:
<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>

surfie
10-24-2003, 02:33 PM
Thanks:D

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum