Error: JavaScript algorithm and data structures certification

In the Drop it exercise of Intermediate Algorithm Scripting. The checks are faulty.

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

These two checks doesn’t make sense and that is stopping me from finishing that exercise. Can someone raise this issue and get it fixed?

Where is your code? Wrap it in triple backticks

In the first case, the passed in function says that it should return only elements of the array where it equals 1. But it is expecting at the end to return an array of [1,0,1]. Why would 0 be in that array?

In the second case, it is asking to return only elements which are larger than 2 but the end array contains 2. Isn’t that wrong?

function dropElements(arr, func) {
    const newArr =[]
    arr.forEach(el=> {
        const val = func(el);
        val && newArr.push(el);
  return newArr;

Here is my code though.

I think you’re filtering out everything when you’re only supposed to filter from the first element until the passed in argument function is true.

You have this function call failing:
dropElements([0, 1, 0, 1], function(n) {return n === 1;})

Your output is: [ 1, 1 ]

The expected output is: [1, 0, 1]

On the array argument, you need to check every element if it passes the function argument. If not, you remove the first element, then check if it passes again and so on and so forth.

My bad. I didn’t read it right. Thanks a lot for the help. Especially for replying so early. Have an amazing day ahead. Keep rocking!

1 Like

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