Copy Array Items Using slice() -technical problem

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

slice() doesn’t work in IDE in browser

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Basic Data Structures - Copy Array Items Using slice()

Link to the challenge:

Read the description for slice again in the instructions:

“Rather than modifying an array, slice() copies or extracts a given number of elements to a new array, leaving the array it is called upon untouched.”

So if the original array is untouched, then would you want to return it?

1 Like

The slice method is non-mutating. It returns a new “slice” of the array it was called on. Unlike splice which does mutate the array.

Yes, the names are confusing. You really only need to remember what one of them does and use deductive reasoning.

A slice is a piece of the old total. Yes, IRL we can’t really take a piece of the pie and leave the pie unchanged. But with a digital pie, we can make as many copies as we like.

Splicing is a change to the original. You can use “Gene Splicing” as a mnemonic.