Intermediate Algorithm Scripting - Drop it

The second test case goes as follows:
dropElements([0, 1, 0, 1], function(n) {return n === 1;}) should return [1, 0, 1] .

Why is it supposed to return [1, 0, 1]?

0===1 yields false
1===1 yields true
0===1 yields false
1===1 yields true
So the function should return [1,1]

But the test case needs it to be [1, 0, 1]

Same for dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;}) should return [3, 9, 2] .

Why is that? Am I misunderstanding the task?

  **Your code so far**
function dropElements(arr, func) {
  return arr.filter(item => func(item))
}

dropElements([1, 2, 3], function(n) {return n < 3; });
  **Your browser information:**

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

Challenge: Intermediate Algorithm Scripting - Drop it

Link to the challenge:

You are using a filter . That does the whole array. You want to filter until you reach that value that returns true and then stop filtering. Once you get a true back, everything else gets passed. Reread the examples again with that in mind. You can accomplish this with a filter, but that might get a little messy. I don’t know, I don’t remember how I solved this, but there is definitely more than one way. I don’t think filter is the best approach, but it might be worth it just to see how to make it work.

1 Like

The second test offers the clearest example of what is happening:

dropElements([0, 1, 0, 1], function(n) {return n === 1;}) should return [1, 0, 1].

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.