Your friend is trying to write a function to accomplish the following transformations:
let x = [3, 3, 3, 3, 3, 3, 3]
// Each time x is called, the following results are shown:
change(x, 0) // [3, 3, 3, 3, 3, 3, 3]
change(x, 1) // [3, 2, 2, 2, 2, 2, 3]
change(x, 2) // [3, 2, 1, 1, 1, 2, 3]
change(x, 3) // [3, 2, 1, 0, 1, 2, 3]
Note: The change() function should not mutate the original array. After each call to the function, the original x should still equal [3, 3, 3, 3, 3, 3, 3].
He comes up with the following code:
function change(x, times) {
for(let i = 0; i < x.length; i++) {
let j = 1;
while (j <= times) {
if (i >= j && i < x.length-j) {
x[i]--;
}
j++;
}
}
return x;
}
Oops! The code appears to mutate the original array. Fix this incorrect code so that the function no longer mutates the original array.
See below:
let x = [3, 3, 3, 3, 3, 3, 3]
// What we want:
change(x, 2) => [3, 2, 1, 1, 1, 2, 3]
change(x, 2) => [3, 2, 1, 1, 1, 2, 3]
// What we get:
change(x, 2) => [3, 2, 1, 1, 1, 2, 3] // Good so far...
change(x, 2) => [3, 1, -1, -1, -1, 1, 3] // Array is mutated :(