Drop it-not sure why am I not passing

Tell us what’s happening:
Not sure why my code is not passing bellow two conditions of the test:
Round two:
dropElements ([0, 1, 0, 1], function(n) {return n === 1;})
My code returns [1,1]. What is wrong with the result?
Round six:
dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;})
My code returns [3,9]. What is wrong with the result?

Many thanks in advance.

Your code so far


function dropElements(arr, func) {
let newArr=arr.filter(x=> func(x))
console.log(newArr)
return newArr;
}

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

Your browser information:

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

Challenge: Drop it

Link to the challenge:

Can you describe what you think your code does?

Do you understand what this part does?

I agree one has to read challenge description twice to get it, but challenge asks you to drop only elements before the first successful. Your function drops all unsuccessful elements.

1 Like

You are correct! The function should stop on first occurrence when condition is met. I missed that part in the assignment. Thanx

It passes argument to the function that sets the conditions I would say. It then takes result of this function as a condition to filter. Filters out all the elements that fulfill criteria and puts them into new array, It’s actually pretty elegant, I think.

1 Like

it doesn’t do as required, it doesn’t matter much if it is elegant or not (but yes, it is)

1 Like

Hi, I am not insisting it does, just saying that it is my understanding of it. It also appears to be working. If there is something I got wrong, I would appreciate your insight.
Thanx.

the challenge asks you to drop everything before the first element that passes the “truth test”, and keeps everything after

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

dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;})
                    ↑

the arrow points the first element passing the test n === 1 in first case and n > 2 in second case, you should return the array with everything before that element excluded, but that element and everything after included, so you should return [1, 0, 1] and [3, 9, 2]

Instead you are returning [1, 1] and [3, 9], keeping only the elements that pass the test
so, you can’t use the filter method

1 Like

Thanx! Appreciate your time for this matter.