Copy Array Items Using slice()

Copy Array Items Using slice()
0.0 0

#1

Tell us what’s happening:

Hello , can’t understand wht’s wrong . We have to Modify the function using slice() to extract information from the argument array and return a new array that contains the elements ‘warm’ and ‘sunny’.

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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

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


#2

and return a new array


#3

What do you mean new Array ? You want me to write

 arr.slice(2,4);
  return newArr;

like that ?


#4

Function slice will not change the value of array arr (non-mutating function), so you have to assign the result to a new var newArr = arr.slice(2, 4) and return it.
In such a case it would help you if you would use console.log and developer tools’ console to see the vars values you get.