Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

NaN error on subtraction of selectedIndex

Posted on 2006-07-13
10
Medium Priority
?
382 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
[X]
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
10 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 750 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 750 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

618 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