/    Sign up×
Community /Pin to ProfileBookmark

NaN error when subtracting

I am trying to develop a simple script that will return the age of a certain person by subtraction their birth year from the current year, but i get a NaN error every time i run the script. My code is below:

var id
var bYear = new Array( 1989, 1991, 1995, 2000 );
var now = new Date();
var ynow = now.getYear();
var age = ynow – bYear[id];
id = prompt( “Find age of…”, “Enter ID No. Here” );
alert( age );

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@jaegernautMar 11.2004 — If I remember correctly the getYear() function only returns a 2 digit year vs. a 4 digit year. For the 4 digit year use getFullYear().

You also may be running into a problem in that you are using the variable "id" before your prompt is setting the value of it.

See if that works for you.
Copy linkTweet thisAlerts:
@js_newbieauthorMar 11.2004 — i tried that, but it still didn't help. im still getting "NaN' when i view the variabe 'age'. here is my exact source code, copied directly from the app.

var id

var fBadge = new Array( "John", "Jane", "John", "Jane", "John", "Jane", "John" );

var lBadge = new Array( "Doe", "Doe", "Smith", "Smith", "Armstrong", "Armstrong", "MacDougal" );

var bYear = new Array( 1900, 1901, 1902, 1989, 1991, 1995, 2000 );

var bMonth = new Array( 05, 01, 02, 02, 09, 11, 06 )

var bDay = new Array( 14, 16, 21, 28, 06, 12, 06 )

var now = new Date();

var fynow = now.getFullYear();

var age = (fynow - bYear[id]);

var id = prompt( "Find information for...", "Enter ID No. Here" );

var bInfo = bMonth[id] + "." + bDay[id] + "." + bYear[id];

alert( lBadge[id] + "," + fBadge[id] + "nBirth Date: " + bInfo + "nAge: " + age );
Copy linkTweet thisAlerts:
@jaegernautMar 11.2004 — You are still using the "id" variable before putting a value to it.

Switch these two lines:

var age = (fynow - bYear[id]);

var id = prompt( "Find information for...", "Enter ID No. Here" );

So they look like this:

var id = prompt( "Find information for...", "Enter ID No. Here" );

var age = (fynow - bYear[id]);

The logic being that you declared the variable but didn't set a value, so when you hit the age = (fynow - bYear[id]); line the id variable just has whatever garbage is in there when you declared it without a value.

So, by flipping the order, you set the id variable from the prompt and the next line will now have a good value to use as the array index.
Copy linkTweet thisAlerts:
@js_newbieauthorMar 11.2004 — hey, it works! ?

thanks a million
Copy linkTweet thisAlerts:
@jaegernautMar 11.2004 — Anytime. Happy to help.
×

Success!

Help @js_newbie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...