0

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'));
}
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." Commented Aug 10, 2013 at 3:35
  • 1
    This code is incomplete in the sense that it references things we can't see. Could you construct a self-contained example in jsfiddle.net that malfunctions in a similar way and post the link? 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. :) Commented Aug 10, 2013 at 5:08
  • From comments (PRPGFerret) it looks like he found an answer. Commented Aug 10, 2013 at 5:45

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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

4 Comments

Hum....Unfortunately, anything there does work. I've found that erasing "m_t2" makes possible to see the resulting array, so I think the issue has a lot to do on how that variable is created.
@ViceNocillator check my solution above .. that should help you solve the problem. Hope that helps :) . Boy did this one take a lot of time . Let me know if it helped you solve it :)
I KNOW WHAT WAS HAPPENING! The m_t2 variable was empty. It was used in other function that I should have called before in the html.
But I'll improve the code adding the solution provided by PRPGFerret. Thanks a lot for all your answers, guys!
0

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

var arr_error_P_t2=[1]

1 Comment

Would help better to indicate to which array you're referring in your answer
-1

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

1 Comment

Humm.. I thought it was a way to be sure that I would be managing numbers. Someone told me that javascript could be quite a bit tricky in than sense...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.