Why this code is giving error (slice)?

  **Tell us what's happening:**

So, I’ve actually passed this test already. However, I tried to improve the function to accept two extra arguments (begginingSlice and endSlice) making this function reusable. Yet, when i try to use the function it doesn’t return the expected result. So I’m just trying to understand what is happening here

  **Your code so far**

function nonMutatingSplice(cities, begginingSlice, endSlice) {
// Only change code below this line
return cities.slice(begginingSlice, endSlice);

// Only change code above this line
}
var inputCities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
nonMutatingSplice(inputCities, 0, 3);
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36

Challenge: Remove Elements from an Array Using slice Instead of splice

Link to the challenge:

what is the value returned? what is the expected result? what error are you getting?

1 Like

The value returned is the exactly array passed.

The expected is a array containing only the 3 first elements of the passed array.

There’s no error, is just not behaving as I expect.

If i do return cities.slice(0,3) it works properly.
While if i do return cities.slice(begginingSlice, endSlice) and pass 0 and 3 as values to begginingSlice and endSlice respectively, It does not work. So I’m assuming that I understood something wrong, and I don’t want to move forward without clearing what I got wrong.

it is returning the first three elements

how are you checking the output of the function?

1 Like

oh my, for some reason I was think that inputCities was using the nonMutatingSplice() and modifying, so i was logging inputCities with returned the original Array.

However, now I’ve found a new error: Even though is returning the expected outcome ([ 'Chicago', 'Delhi', 'Islamabad' ]) the exercise is returning the error:

nonMutatingSplice(["Chicago", "Delhi", "Islamabad", "London", "Berlin"]) should return ["Chicago", "Delhi", "Islamabad"].

this function call doesn’t have arguments for begginingValue and endValue, the version of the function you have written can’t pass the tests

Oh, now I uderstand. I appreciate your help.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.