Copy Array Items Using slice()

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

and return a new array

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

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

like that ?

1 Like

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.

2 Likes

or …

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

… all on one line works

1 Like

Dude I simply wrote down your answer because I passed almost one hour trying to understand this question. I have to have a review on the basics, principally on assignments. cheers.

No, I did not get the idea.

actually, that question was the order of arrays in terms on the slice(). This one is assignments, I just realize I should revise some concepts of basic JS. :disappointed_relieved: