admin管理员组

文章数量:1405768

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'));
}

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'));
}
Share edited Aug 10, 2013 at 3:58 Harry 89.9k26 gold badges214 silver badges223 bronze badges asked Aug 10, 2013 at 3:30 ViceNocillatorViceNocillator 1051 gold badge3 silver badges11 bronze badges 4
  • "The Number() function converts the object argument to a number that represents the object's value. If the value cannot be converted to a legal number, NaN is returned." – Sterling Archer Commented Aug 10, 2013 at 3:35
  • 1 This code is inplete in the sense that it references things we can't see. Could you construct a self-contained example in jsfiddle that malfunctions in a similar way and post the link? – Paul Commented Aug 10, 2013 at 4:42
  • @Paul he is definitely accessing a array element that doesn't exist. See my answer below. Doing a fiddle is a good thing but I think that should solve the problem. :) – woofmeow Commented Aug 10, 2013 at 5:08
  • From ments (PRPGFerret) it looks like he found an answer. – Paul Commented Aug 10, 2013 at 5:45
Add a ment  | 

5 Answers 5

Reset to default 2

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.

Instead using new Number(), just multiply your value with 1 (your_val * 1). Its the easiest and fastest way to convert a value into integer.

function calculation_errors() {
    arr_error_P_t2 = [];
    for (var i = 0; i < arr_P_t1.length; i++) {
        var K = arr_K_t1[i] * 1;
        var P = arr_P_t1[i] * 1;
        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'));
}

Don't convert the numbers into objects, and that should solve your NaN issue.

function calculation_errors(){      
    arr_error_P_t2=new Array();

    for(var i=0;i<arr_P_t1.length;i++){
        var K= arr_K_t1[i];
        var P= arr_P_t1[i];      
        arr_error_P_t2.push((Math.sqrt(1+Math.pow(m_t2,2)))*(Math.sqrt((Math.pow(1/K,2)+(Math.pow(1/P,2)))))); 
        //I prefer .push() but you don't have to use this part
    }

    alert(arr_error_P_t2.join('\n'));
}

You must be insert at least one item inside that array.

var arr_error_P_t2=[1]

Why do you have to use "new Number()"? It would be an object if you do that. Objects cannot be printed directly.

本文标签: Mistake in javascript with NaN in an arrayStack Overflow