Debug
Your solution was not working because you weren't assigning the new value into the array
It will works if you use the following code to do this :
numDouble[i] = numDouble[i] * 2;
By the way, there is no need to check if the value is 0 because 0 * 2 still equal 0 which works perfectly. so you could have removed the if(arr[i]!=0) part.
Also, when you're doing let numDouble = arr; you aren't creating a new array but assigning the existing array reference into the variable.
If you want to create a new array you can use the spread operator which is used for example to duplicates array.
Example : let numDouble = [...arr];
You would have checked if there was a division that might cause a problem
Example :
function duplicate(arr) {
let numDouble = [...arr];
for (let i = 0; i < arr.length; i++) {
numDouble[i] = numDouble[i] * 2;
}
return numDouble;
}
const arr = [1, 2, 3, 4]
const newArr = duplicate(arr)
console.log(newArr)
Another solution
Another solution that might works better is the map function which take a function an apply it to every item of the array
So for example you could do this :
const arr = [1, 2, 3, 4]
const multiplyByTwo = function(number) {
return number * 2
}
console.log(arr.map(multiplyByTwo))
Or even with one line of code using arrow functions :
const arr = [1,2,3,4]
console.log(arr.map(x => x*2))