Looks like the starter code’s written with the while loop
function copyMachine(arr, num) {
let newArr = [];
while (num >= 1) {
// Only change code below this line
// Only change code above this line
num--;
}
return newArr;
}
console.log(copyMachine([true, false, true], 2));
Splice and there being nothing at index 0 is not the problem. The following code works just fine:
function copyMachine(arr, num) {
let newArr = ;
while (num >= 1) {
newArr.splice(0, 0, […arr]);
num–;
}
return newArr;
}
console.log(copyMachine([true, false, true], 2));
The problem appears to occur as a result of attempting to assign a value to the array using =. What I don’t understand is why. Even though the assignment operator is unnecessary it should still work, right?
Splice performs an operation on an array, so there’s no need to do newArr = newArr.splice(0, 0, adder);, once you write newArr.splice(0, 0, adder); the array will be modified. Using the assignment operator will return an empty array.