I'm quite new to javascript and I can't find out at which point an undefined object is inserted into my array:
function foo(arrA){
if(!arrA || arrA[0] === 0)
return undefined;
let arrB = [];
let prevEntry = 1;
for(let i=0; i < arrA.length; i++) {
if (2*prevEntry == arrA[i]) {
prevEntry *= 2;
arrB[i] = (Math.pow(arrA[i], 2)).toString();
}
}
return arrB;
}
foo([2,4,6,8,9,15,16,27]);
As output I get an array with powers of 2, as expected, but it's interlaced with undefined objects and I just don't see why.
(7) ["4", "16", undefined × 1, "64", undefined × 2, "256"]
Thanks for your help in advance! :-)
if (2*prevEntry == arrA[i]){This line.