Remove an Item from an Array helppp

i dont understand how am i wrong

Your code so far


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
      let a = [...state];
      a.slice(action.index,1);
      return a;
    default:
      return state;
  }
};

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

const store = Redux.createStore(immutableReducer);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/redux/remove-an-item-from-an-array

Hi @Hamzah98

You’re not assigning the result of calling a.slice... as it returns a new modified version of the array, so you would want to do something more like:

      let a = [...state];
      a = a.slice(action.index,1);
      return a;

Alternatively, because slice does return a new modified array (so doesn’t modify the state), you could just return the result of calling .slice without the intermediary call to the spread operator, for a more succinct reducer:

      return a.slice(action.index,1);

i tried directly returning,not working either

I think you need to use slice twice to get the left side and the right side of the array relative to the removed item. You can concat them afterwards. Pity you cannot just use the splice method as it returns the deleted item, not the modified array.

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
            let a = [...state];
            return Array.prototype.concat(a.slice(0,action.index),a.slice(action.index + 1, a.length));
        default:
            return state;
    }
};

const removeItem = (index) => { //// creates an object with an index property that specifies what should be removed
    return {
        type: 'REMOVE_ITEM',
        index
    }
} 

console.log(immutableReducer([1,2,3,4,5,6], removeItem(2)));