admin管理员组文章数量:1302871
I am trying the get the maximum value in an array of numbers:
maxtime=Math.max.apply( Math, cnttimearr );
alert(maxtime);
However, I am getting NaN
instead of the maximum value. Can anyone tell me what I am doing wrong?
I am trying the get the maximum value in an array of numbers:
maxtime=Math.max.apply( Math, cnttimearr );
alert(maxtime);
However, I am getting NaN
instead of the maximum value. Can anyone tell me what I am doing wrong?
-
2
What is
cnttimearr
? – p.s.w.g Commented Jan 1, 2014 at 12:15 -
write definition of
cnttimearr
also – AmanS Commented Jan 1, 2014 at 12:27
2 Answers
Reset to default 5Read the manual.
If at least one of arguments cannot be converted to a number, the result is
NaN
.
Make sure all the elements in the array are convertible to numbers.
> xs = [1, 2, '3'];
[1, 2, "3"]
> Math.max.apply(Math, xs);
3
> xs = [1, 2, 'hello'];
[1, 2, "hello"]
> Math.max.apply(Math, xs);
NaN
Check out your array cnttimearr
that all the values should be convertible to numbers
cnttimearr= [5, 6, 2, 3, 7]; /*your array should be like this */
maxtime = Math.max.apply(Math, cnttimearr);
/* This about equal to Math.max(cnttimearr[0], ...) or Math.max(5, 6, ..) */
alert(maxtime);
本文标签: How to avoid NaN when using the Mathmax() function in JavaScriptStack Overflow
版权声明:本文标题:How to avoid NaN when using the Math.max() function in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741741680a2395312.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论