Mistake in javascript with NaN in an array -
when execute function in javascript, nan result. seems quite ilogical beacuse arrays employed in operation numerical , shown when alert(); left code here 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')); }
the reason getting nan
because array arr_k_t1
has length smaller array arr_p_t1
.
in for loop
: trying array element greater own size statement
var k= arr_k_t1[i];
it returns undefined
(because have exceeded number of elements in arr_k_t1
. in javascript returns undefined
if try access non-existent element of array.
then doing mathematical operations on return nan
(the result have got).
the solution :
function calculation_errors(){ arr_error_p_t2=new array(); //comment : assuming array "arr_k_t1" atleast of length equal //array "arr_p_t1" in loop follows //this wrong assumption , leading "nan" @ end !! ... ... ...rest of code
edit : haven't been able include remaining code because making post weird. problem lies in for loop
accessing element not exist.
Comments
Post a Comment