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.99 Safari/537.36.

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

you just need to return what slice is returning
right now you are returning the original param ‘arr’

1 Like

Ok and can code I can do.?:face_with_raised_eyebrow::face_with_raised_eyebrow:

I recommend you use console.log to see what your code is doing. :slight_smile:

Nooo I don’t remember it sorry.

let myVariable = " something";

that’s how you declare a variable called ‘myVariable’ and assign a string to it for example…

I should right that ?

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

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

I should remove arr from arr.slice()…

cold,rainy,warm,sunny,cool,thunderstorms
that?

Warm and sunny?..

function forecast(arr) {
// change code below this line
let myArr = arr.slice(2, 4);
return arr = [“warm”, “sunny”];
}

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

I got it.It is very simple.

No this is right.We do it together I ask you for help and you help me I am not cheating we do it together.

Here’s the code you wrote earlier.
If you return arr, that is not correct because you are returning the original array without changing it, right?

If you return [‘warm’,‘sunny’] that is also not correct (even if it passes the challenge) because you are ‘hard-coding’ an answer. The word ‘hard-coding’ means when you write the answer exactly instead of using what you learned to make the function give the answer.

So what else is left?
The whole function only has 2 possible variables.
We said ‘arr’ is not correct.
So what else can you return?

Try doing this insert a few consol.logs to see whats going on I think your really close.

// change code below this line
let myArr = arr.slice(2, 4) ;
console.log("This is the content of myArr = " + myArr);
console.log("This is the content of arr = " + arr);
return arr;
}
// do not change code below this line

So you see the myArr contains the solution. Now you just need to set arr equal to myArr you are close keep trying.