Basic Data Structures: Copy Array Items Using slice(), please

Hey guys, i really dont understand this assignment.

When i usually dont understand something is, of course, i click for help, look at the answer and type it over and “translate” it into English.

However, this assignment i really dont understand…

/////////////////////////////////////
////////////////////////////////////
///////////////////////////////////

function forecast(arr) {

  // Only change code below this line

  return arr.slice(2,4);

}

// Only change code above this line

console.log(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']));

/////////////////////////////////////
////////////////////////////////////
///////////////////////////////////

What i understand is that in the arr.slice(2,4); it means they find the third element in the array: and delete the next 4 elements). So i dont know how this works.

The previous assignments seem to work differently. I dont know how the array comes up with [warm, sunny]; so what kind of thinking mistake do i make here?

Thanks a lot for your help guys!

slice makes a copy of the array keeping items from index 2 (included) to index 4 (excluded), the indexes work differently than in splice

also, slice doesn’t change the array on which it is used


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Hey @Layilah!

Might I suggest that for the next section on algorithms you try to refrain from looking at the solution for help. I know that programming is new and can be tough sometimes but it is even more rewarding when you finally solve the problem. Also, when you get stuck you can always ask the forum for help.

I would also suggest maybe keeping a second tab open with mdn docs when trying to solve problems. I used that a lot with both algorithm sections and honestly I don’t think I would have been able to get through it without it.

Hope that helps!

Hi, J Wilkins,

Thank you for the tips for using mdn docs. I also think its nice to check there once in a while.

However, if we don’t use the help function on FCC, what do you think we should be using it for?

So, how do you sue mdn docs specifically?

you use the hints if you need to do, and look at the solution(s) only after solving the challenge

writing code, and understanding code are two different skillsets, you are not developing the first if you just refer to the solutions as soon as you have difficulties. Instead of using the “Get a hint”, use the “Ask for help” part of the help feature - here in the forum people can help you write code just giving hints and point out errors so that you actually develop your ability to write code

1 Like

For this problem specifically, if I didn’t understand the slice method then I would google slice javascript. The first two results are for mdn. Since we are dealing with arrays then I would click on the array article and look through the definition and examples. If the explanation doesn’t work for you then the good news is there are tons of other articles covering the exact same topic.

You could look into w3 schools, javascripttutorial, geeks4geeks, youtube and freecodecamp news.

Here is a great article talking about the differences between slice, splice and split.

All of this incredible information is from one google search.

I hope you try this approach moving forward because you will learn a lot through the process. I got stuck in both of the algorithm sections and functional programming section because I am beginner too. It took me forever to understand the map method and callback functions. I probably looked at dozens of articles and youtube videos but it finally clicked for me with repeat exposure and practice.

Hope that helps!

Happy coding!

HEy, i did so, and devdocs, is really seary to understand and cool, thanks a lot, it helped me out, easily.

However, i have to say, when i make a my code, and enter it, it doenst always catch it in the fcc browser, so i think it is useful to check the solution, otherwise you keep searching, while you have been on the right track long before you know its the right answer…

you can always ask for help here on the forum, what if there was something you missed?