Semantic problem: Copy Array Items Using slice()

Why does adding a line of arr.slice(2,4) instead of directly return arr.slice(2,4) will output the original array in console.log()?

Your code so far


function forecast(arr) {
  // change code below this line
  arr.slice(2,4);
  return arr;
}

// do not change code below this line
console.log(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/copy-array-items-using-slice

slice does not mutate the array, it returns a (shallow) copy. splice on the other hand does mutate the array.

2 Likes