~Basic Data Structures: Copy Array Items Using slice()

Tell us what’s happening:
i am doing it right but it is not being accepted by as a correct answers, i have tried many times plz help
Your code so far

function forecast(arr) {
// Only change code below this line
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/83.0.4103.61 Safari/537.36.

Challenge: Copy Array Items Using slice()

Link to the challenge:

Hey @sobia8040,

Look at what the challenge said you did wrong:

Try to console.log(forecast()); and see what it returns.

slice doesn’t work like splice:

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

thanks for replying, i have tried this console.log(forecast()); it does not work either in that case it says forecast is not defined.

I’m sorry if I confused you a bit, but @ieahleen has explained it well right there. .slice(); doesn’t work like .splice();.The .splice(); modifies the array and then returns the modified array, while .slice(); grabs the array and slice it, but the difference is that it creates a NEW array. If you don’t assign it like this:

let arr = [1, 2, 4, 6];
let arr1 = arr.slice(2,6);

It will just slice, create a new array and ignore it.

As already explained, slice does not mutate the array. You have to use the return value.

const getBrian = () => {
  return ['Life', 'of', 'Brian'].slice(2, 3);

console.log(getBrian()); // [ 'Brian' ]
1 Like

thanks. i got it now