Remove Elements from an Array Using slice ,Instead of splice

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

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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 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

It should start at 0 index. and then end after 3 items are added.

So i should write var x= cities.slice(0,3);right?

1 Like

arr.slice(start, end)

https://www.w3schools.com/jsref/jsref_slice_array.asp

The goal is to have your function ouput be

["Chicago", "Delhi", "Islamabad"]

Using your current code, it is:

["London"]

Let’s say we were changing your slice to the following:

Cities.slice(0,4);

the output will be:

["Chicago","Delhi","Islamabad","London"] 

But we only want the first 3 cities.

Yes that must do it.

i rite that but tell me arr is not defined

This should work.
just add return x at end.

arr just means what ever array you have, in this case it would be ‘cities’

so… cities.slice(0,3);

Thank you for helping.

1 Like

var x= cities.slice(0,3);
return x

function nonMutatingSplice(cities) {
// Add your code below this line

return cities.slice(nonMutatingSplice, 3);

// Add your code above this line
}
var inputCities = [“Chicago”, “Delhi”, “Islamabad”, “London”, “Berlin”];
nonMutatingSplice(inputCities);