My solution: Remove an Item from an Array

My solution: Remove an Item from an Array


What is wrong with my code. It looks to work as asked. But it does not pass the tests.

const immutableReducer = (state = [0,1,2,3,4,5], action) => {
  switch(action.type) {
    case 'REMOVE_ITEM':
      // don't mutate state here or the tests will fail
      if (action.index) {
      const newState = [...state];
      newState.splice(action.index, 1);
      return newState;
      } else {
        return state;
      return state;

const removeItem = (index) => {
  return {
    type: 'REMOVE_ITEM',

const store = Redux.createStore(immutableReducer);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.25 Safari/537.36.

Link to the challenge:


You code fails when store.dispatch(removeItem(0)) is called and then write store.getState(), it should be [1, 2, 3, 4, 5], but your solution returns [0, 1, 2, 3, 4, 5].

What are you trying to accomplish with the following line?

if (action.index) {

If action.index is 0, the above line will evaluate to false, so you end up just returning the initial value of state without removing the first element.


Hi @randelldawson,

Thanks for your advice.

I used the if conditinal to see if I could except the case in that removeItem() is called without argument… But I understand now that this is not possible since I declared removeItem to ask at lease for the argument index.

Your explanation regarding index 0 explains all my confusion.