Remove Elements from an Array Using slice ,Instead of splice

Remove Elements from an Array Using slice ,Instead of splice
0

#1

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


#2

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


#3

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


#4

arr.slice(start, end)

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


#5

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.


#6

Yes that must do it.


#7

i rite that but tell me arr is not defined


#8

This should work.
just add return x at end.


#9

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

so… cities.slice(0,3);


#10

Thank you for helping.


#11

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