Redux : Remove an Item from an Array challenge help

Redux : Remove an Item from an Array challenge help
0

#1

Tell us what’s happening:
Im not sure why this is not passing the test. Am I not allowed to use filter() on this challenge?
any feedback will be appreciated.

Your code so far


const immutableReducer = (state = [0,1,2,3,4,5], action) => {
  switch(action.type) {
    case 'REMOVE_ITEM':
      const newState = state.filter( val => val !== action.index );
     
      return newState;
    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 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

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


#3

Thank you for the hint .


#4

Your code must delete newState[action.index],but not a number.

I cant think up better, but you may finish the test with it. How can i do it with slice or concat methods?

let newState = [...state];
newState.splice(action.index, 1);
return newState;

#5

This is because you are not getting the value you think you are when using filter

// val is the actual item in the index
const newState = state.filter(val => val !== action.index );

The filter callback has a few more parameters that you could make use of. Take a look at the screenshot from mdn below


#6

As JM-Mendez mentioned your filter function is wrong. Filter method works for “value” filtering not index.
To filter the array by index you have to use:

return state.filter( item=> item != state[action.index]  )

#7

This was the slice/concat method.
return state.slice(0, action.index).concat(state.slice(action.index + 1, state.length));

but yes if you look at the link shared by JM-Mendez you can use the filter method.


#8

Your solution is way more consice than mine.
My original solution state.slice(0,action.index).concat(state.slice(action.index+1)).
Your solution corrected state.filter((v,i)=>i!=action.index)