[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

NaN error on subtraction of selectedIndex

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
894359
Asked:
894359
2 Solutions
 
RoonaanCommented:
Use:

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

-r-
0
 
894359Author Commented:
This is what is actually failing:

age.setMonth(document.pollform.month[document.pollform.month.selectedIndex].value-1);
0
 
ZvonkoSystems architectCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
vikrant4u2Commented:
try using the way roonaan has suggested.

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

0
 
ZvonkoSystems architectCommented:
Your var age is NOT an Object of type: Date
0
 
894359Author Commented:
It is I just didn't post the whole code!

var age = new Date();
0
 
ZvonkoSystems architectCommented:
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
 
894359Author Commented:
I found the issue. it was on the getting of the dates

age.getMonth vs age.getMonth()



0
 
894359Author Commented:
I should have stated my question better! :(
0
 
ZvonkoSystems architectCommented:
You can ask for delete for this question here: http:/Community_Support/askQuestion.jsp
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now