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

The slice method does not mutate the array being sliced. Instead it returns a new array of element which were sliced. You need to either declare a new variable to assign the return of the slice or just return the slice.

2 Likes

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:

Do you remember how to assign a variable a value? If so, then try assigning what you wrote on the line containing the slice method and assign it to a new variable which you will declare and then return that new variable.

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…

If you are not able to assign a value to a variable, I strongly suggest you start the Basic JavaScript section over again. This is a very fundamental task and if you do not understand how to do it, you need to make sure you do before moving forward with any more JavaScript challenges.

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

You successfully assigned the slice to myArr, but for some reason, you are returning arr.

1 Like

I should remove arr from arr.slice()…

What do you think the above line does in your code?

What do you think the variable arr contains?

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

Yes, you are correct, so you should not be returning arr. Which variable now contains ‘warm’ and ‘sunny’ elements? Return that one instead.

1 Like

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, that is not correct. You are cheating by hardcoding the correct answer.

What does the variable myArr contain?

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?