Tell us what’s happening:
After 12 failed attempts to pass this challenge, I had to see solutions which lead me to some confusion:
The solution:
return state.slice(0, action.index).concat(state.slice(action.index + 1, state.length));
I do understand the first part of slice()
-ing before concat()
, but for the the second part of code (inside concat
's parameter) I got 2 question:
-
What does the
+ 1
means in thebeginIndex
(first parameter) of the.slice
method (*I know it adds +1 to the index number, but I mean what’s the logic behind it)? -
Which
.length
of the state is theendIndex
(second parameter) referring to - the current (original/non-modified) state or the modified shallow copy of the current state (to whom we are concatenating)?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
return
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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36
Challenge: Remove an Item from an Array
Link to the challenge: