Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 10-23-2014, 12:29 PM   #1
Baseband Member
Join Date: Jul 2014
Location: USA
Posts: 29
Default HTML Programming Language

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:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Variables and Names</title>
<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>

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");


megan1989 is offline   Reply With Quote
Old 10-23-2014, 06:17 PM   #2
Site Team
berry120's Avatar
Join Date: Jul 2009
Location: England, UK
Posts: 3,434
Default Re: HTML Programming Language

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.

Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 10-24-2014, 12:42 PM   #3
Baseband Member
Join Date: Jul 2014
Location: USA
Posts: 29
Default Re: HTML Programming Language

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!
megan1989 is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 07:14 AM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0