使用数组中的NaN在javascript中出错(Mistake in javascript with NaN in an array)

编程入门 行业动态 更新时间:2024-10-28 00:18:35
使用数组中的NaN在javascript中出错(Mistake in javascript with NaN in an array)

当我在javascript中执行此函数时,我得到NaN结果。 看起来非常不合逻辑,因为在这个操作中使用的数组都是数字的,并且在我使用Alert()时正确显示; 我把代码留在这里供你监督:

function calculation_errors(){ arr_error_P_t2=new Array(); for(var i=0;i<arr_P_t1.length;i++){ var K=new Number(arr_K_t1[i]); var P=new Number(arr_P_t1[i]); arr_error_P_t2[i]=(Math.sqrt(1+Math.pow(m_t2,2)))*(Math.sqrt((Math.pow(1/K,2)+(Math.pow(1/P,2))))); } alert(arr_error_P_t2.join('\n')); }

When I execute this function in javascript, I get a NaN result. It seems quite ilogical beacuse the arrays employed in this operation are all numerical and are shown properly when I do so with a Alert(); I left the code here for your supervision:

function calculation_errors(){ arr_error_P_t2=new Array(); for(var i=0;i<arr_P_t1.length;i++){ var K=new Number(arr_K_t1[i]); var P=new Number(arr_P_t1[i]); arr_error_P_t2[i]=(Math.sqrt(1+Math.pow(m_t2,2)))*(Math.sqrt((Math.pow(1/K,2)+(Math.pow(1/P,2))))); } alert(arr_error_P_t2.join('\n')); }

最满意答案

获得NaN的原因是因为您的数组arr_K_t1的长度小于数组arr_P_t1 。

在你的for loop :你试图通过语句获得一个更大的数组元素

var K= arr_K_t1[i];

它返回undefined (因为你已经超过了arr_K_t1的元素arr_K_t1 。所以在javascript中,如果你试图访问一个不存在的数组元素,它会返回undefined 。

然后你正在对它进行数学运算,这显然会返回NaN (你得到的结果)。

解决方案是这样的:

function calculation_errors(){ arr_error_P_t2=new Array(); //COMMENT : You are assuming array "arr_K_t1" is atleast of length equal to //array "arr_P_t1" in the for loop that follows //THIS IS A WRONG ASSUMPTION AND IS LEADING TO THE "NaN" at the end !! ... ... ...rest of your code

编辑 :我无法包含剩余的代码,因为它使帖子很奇怪。 然而问题在于他正在访问不存在的元素的for loop 。

The reason you are getting a NaN is because your array arr_K_t1 has a length that is smaller than your array arr_P_t1.

In your for loop : You are trying to get an array element that is greater its own size with the statement

var K= arr_K_t1[i];

it returns undefined (because you have exceeded the number of elements in arr_K_t1. So in javascript it returns undefined if you try to access a non-existent element of an array.

Then you are doing mathematical operations on it which obviously return NaN (the result that you have got).

The solution is this :

function calculation_errors(){ arr_error_P_t2=new Array(); //COMMENT : You are assuming array "arr_K_t1" is atleast of length equal to //array "arr_P_t1" in the for loop that follows //THIS IS A WRONG ASSUMPTION AND IS LEADING TO THE "NaN" at the end !! ... ... ...rest of your code

Edit : I haven't been able to include the remaining code because its making the post weird. However the problem lies in the for loop where he is accessing an element that does not exist.

更多推荐

本文发布于:2023-07-14 17:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106046.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   NaN   javascript   Mistake   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!