Solved

NaN error on subtraction of selectedIndex

Posted on 2006-07-13
10
375 Views
Last Modified: 2008-02-26
Why would the below code return NaN:

alert("DOES NOT WORK - RETURNS NaN:"+document.pollform.month[document.pollform.month.selectedIndex].value - 1);

if I remove the subtraction it works fine???

alert("WORKS:"+document.pollform.month[document.pollform.month.selectedIndex].value - 1);
0
Comment
Question by:894359
10 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 250 total points
ID: 17097322
Use:

alert("DOES NOT WORK - RETURNS NaN:"+(document.pollform.month[document.pollform.month.selectedIndex].value - 1));

-r-
0
 

Author Comment

by:894359
ID: 17097356
This is what is actually failing:

age.setMonth(document.pollform.month[document.pollform.month.selectedIndex].value-1);
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17097363
Check this:

    alert("WORKS:"+(document.pollform.month.value-1))


One question for explanation: does JavaScript evaluate the statement expression from left to right or right to left?
And if it is from left to right, what number should yield this:  ""DOES NOT WORK - RETURNS NaN: 9"-1;

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 2

Expert Comment

by:vikrant4u2
ID: 17097366
try using the way roonaan has suggested.

i think alert tries to evaluate the msg passed to it.

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17097372
Your var age is NOT an Object of type: Date
0
 

Author Comment

by:894359
ID: 17097377
It is I just didn't post the whole code!

var age = new Date();
0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 250 total points
ID: 17097404
If one of the names, the form or the select, is wrong spelled, like it Month, or PollForm, then you get "undefined": document.pollform.month
Check twice the month select name. Is it perhaps "months"?


0
 

Author Comment

by:894359
ID: 17097443
I found the issue. it was on the getting of the dates

age.getMonth vs age.getMonth()



0
 

Author Comment

by:894359
ID: 17097473
I should have stated my question better! :(
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17098003
You can ask for delete for this question here: http:/Community_Support/askQuestion.jsp
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTTPS jquery doesn't work 9 58
document.getElementById not worj with 2 IDs with the same name 10 39
how can i count words? 2 42
how can i make the array into an object? 4 30
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question