Copy Array Items,Using slice()

Copy Array Items,Using slice()
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.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


#2

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.


#3

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


#4

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


#5

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


#6

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.


#7

Nooo I don’t remember it sorry.


#8

let myVariable = " something";

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


#9

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.


#10

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


#11

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


#12

I should remove arr from arr.slice()…


#13

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

What do you think the variable arr contains?


#14

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


#15

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


#16

Warm and sunny?..


#17

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.


#18

No, that is not correct. You are cheating by hardcoding the correct answer.

What does the variable myArr contain?


#19

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.


#20

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?