Redux challenge not passing but logging correct results

Hey,
I’m currently stuck at this Redux challenge. Basically I just need to remove an item from an array without mutating the original one.
console.log() gives me the correct results but the challenge is still not passing. Is it bugged, or am I missing something? I tried it in Firefox and Chrome.

Here is the relevant code I used (I also tested a solution with the spread operator but that didn’t pass either):

  switch(action.type) {
    case 'REMOVE_ITEM':
      return state.filter(num => num !== action.index);
    default:
      return state;
  }

Thanks in advance!

The problem is here:

return state.filter(num => num !== action.index);

First of all, I don’t think filter is the best way to solve this problem. That is an O(n) operation, whereas if you just use the index to split the array into two chunks, I think that would be O(1). But assuming we want to use filter

The problem is that you are comparing the index you are given with the value. You are supposed to remove by index, not value. Of course, the problem could have been written to remove specific values - in which case you code would work - but it wasn’t written that way. So, your code works in this problem because the indexes are the same as the values. Until you start removing things. then it breaks.

So, how do you get the index? Well, filter sends it to you as the second parameter. So, you could use:

return state.filter((num, index) => ...

and make a small change to the rest of the line.

Thanks for the explanation! I actually thought I have to remove the specified number, but I guess my brain was AFK there… :stuck_out_tongue:

Didn’t know about this O(n) stuff though, now I have something to research. Thanks!

The O(n) stuff is a measure of complexity, sometimes called “big O notation”. In a worst case scenario, an O(1) app would not change speed depending on the size of the app. An O(n) app would grow proportionally as the data (n) grew. You can have n^2, n log n, n!, and lots of others. It’s a way to measure how efficient a solution is. It’s a good thing to learn a little bit about as you grow and learn algorithms. It will really impress interviewers. I wouldn’t worry about diving into it whole hog, but taking a look at some basics and starting to think about it can be helpful.

My point is that to search for a location by index is always a one step process, O(1). It never gets bigger. Using a filter, you may have to go through all of the data. If you have 1,000,000 data points, you may have 1,000,000. What if you had a billion or a trillion? Now imagine if your solution was O(n!). 20! is already more than the number of seconds since the beginning of the universe. That’s why these things matter. But it’s OK and normal if it’s not on your radar yet. I’m just explaining why I think the splicing method is better, even if it won’t matter much in this specific, small case.

That makes sense. I’ll definitely keep that in mind for optimizing stuff. Thanks again!