Intermediate Algorithm Scripting - Drop it

Tell us what’s happening:
I came up with a solution and it passes the challenge, at least the test cases that are required to pass. But the initial arguments proposed in the challenge
dropElements([1, 2, 3], function(n) {return n < 3; } does not pass.

Your code so far

function dropElements(arr, func) {
  let newArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (func(arr[i])) {
      newArr = arr.slice(i);
      break;
    }
  }
  return newArr;
}

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

I understand that the problem is that the for loop breaks as soon as the function condition is satisfied (n < 3).
arr[0] is 1 and the condition is immediately satisfied.
I noticed that all of the test case conditions use ‘greater than’, ‘greater than or equal to’ or ‘strictly equal’. None of the test cases use ‘less than or equal to’.
I know it passes the challenge, but the fact that the default arguments did not pass is really bugging me.
I am very new to coding and I was wondering if there is a way to identify the index of the slice position, regardless of whether the comparison operator is ‘less than’ or ‘greater than’.

I’m not looking to be spoon fed. Any hints or suggestions would be appreciated.

:astonished: Thank you so much for that clarification. I would’ve been struggling with this for days if it wasn’t for you.
I was so fixated on ‘n < 3’ that I thought the return value should be [1, 2].
Like I said, I am very new to this and today I learned that understanding the problem is half the battle, maybe more.
Thanks again!

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