Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

NaN error on subtraction of selectedIndex

Posted on 2006-07-13
10
376 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

839 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