Solved

NaN error on subtraction of selectedIndex

Posted on 2006-07-13
10
373 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now