Remove Elements from an Array Using slice Instead of splice

Tell us what’s happening:
What I’m doing wrong here?

I’ve tried a lot of schemes for this exercise but it’s always missing the last objective to return the values, I don’t know what’s going on… :confused:

Your code so far


function nonMutatingSplice(cities) {
  // Add your code below this line
  var x = cities.slice(3,4);
  return x; 
  // Add your code above this line
}
var inputCities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
nonMutatingSplice(inputCities);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux armv7l) AppleWebKit/537.36 (KHTML, like Gecko) Raspbian Chromium/65.0.3325.181 Chrome/65.0.3325.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/remove-elements-from-an-array-using-slice-instead-of-splice

The instructions want you to return the first three elements of the array.

Sure, but this isn’t what my function is doing? And says it too that “has to return with another variable” but as you can see, nothing happen.

I’ve tried “cities.slice(3)” and “return cities.slice(3,1)” and many others, but I can’t proceed on it.

Arrays in javascript start at 0.

The method slice(a, b) takes two parameters:
a) beginning index (inclusive).
b) ending index (exclusive).

When you code calls slice() it is returning null since the parameters are in the wrong order with slice(3, 1), and returning index 3 and everything after with slice(3).

1 Like

Now you helped me, the instructions on the side was showing me an example that was the reverse of the actual point, thanks! I did it.

That’s true, but the instructions also explain what arguments go into slice. Recall that the slice method takes two arguments for the indices to begin and end the slice (the end is non-inclusive), and returns those items in a new array.

Next time, though I’ll be more helpful. It’s clear i was not this time.

I dont get… before lesson was with slice, and again…
Somehow to easy…