HTML Programming Language

megan1989

Baseband Member
Messages
29
Location
USA
Hello
I am attempting to create a html code that ask the user for their name, how old they were when they first began working on computer, and how old they are now, then displaying in a window.alert how long they have been working on their computer. I have been successful, except that I can't get it to display the name in the alert. It displays the name as NaN, if someone can help me figure out where I went wrong, I'd appreciate it. Thanks and here is the code:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Variables and Names</title>
</head>
<body>
<p>The Computer Years Page<P>
<i> This page calculates the how many years of your lifetime
that you have been working on computers.</i>
</P>

<script>
var name = parseFloat(window.prompt("Your Name:",""));
var firstYear = parseInt(window.prompt
("How old were you when you first started working on computers?:",""));
var age = parseInt(window.prompt("How old are you now?:",""));
var totalYears = age - firstYear;
window.alert(totalYears + " years is how long " + name + " you have worked on computers");
</script>
</body>

</html>
 

berry120

Fully Optimized
Messages
3,434
Location
UK
It looks like you're trying to parse the name as a floating point number - in this case, javascript is then trying to convert the text to a decimal number, and when it realises it can't do this, is displaying NaN (short for not a number) instead.
 

megan1989

Baseband Member
Messages
29
Location
USA
Thanks for pointing that Berry, after I dig a little digging I was able to correctly type out the code and have it do what I wanted it to. I inserted a function, got rid of the parse as yes it is for numbers not strings, which is why I was getting the NaN (not a number). So thanks!
 
Top