0

I have the following code:

function bytesToMb(arr)
{
    for(var i=0;i<arr.length;arr++)
    {
        var mbs= arr[i]/(1000*1000);

        arr[i]=mbs;
    }

    return arr;
}

after the line arr[i]=mbs executes, the value of arr (the array object itself) becomes NAN.
why is that????

3 Answers 3

3

You are incrementing arr, arr + 1 = NaN because array is NaN; you ought to do i++ in your for loop instead...

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

Comments

1

You're using arr++ instead of i++ as the third clause in your for loop.

The type coercion from Array to Number leads to your NaN.

Comments

1

Change arr++ to i++

function bytesToMb(arr) {
    for (var i = 0; i < arr.length; i++) {
        var mbs = arr[i] / (1024 * 1024); // you should use 1024*1024 here to make it more precise if you need to.
        arr[i] = mbs;
    }
    return arr;
}

Comments

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.