While loop, and algorithm DROP IT!(solved)

After I solved Drop it algorithm challenge, I checked this guide trying to find more advanced solution.
The Advanced Code Solution uses while loop to solve the problem. My question is while loop stops when the condition is false. What if the arr is not in numeric order as the following:

function dropElements(arr, func) {
while(arr.length > 0 && !func(arr[0])) {
arr.shift();
}
return arr;
}

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

It returns [2,1,3], which is not the correct result. Anyone can explain to me?

Also, in the challenge explaination, it says “dropElements([0, 1, 0, 1], function(n) {return n === 1;}) should return [1, 0, 1]”. Why this result?

Hello Chelsea-Li,

I think you have missunderstod the problem. Read it again, the first paragraph in particular.

Drop the elements of an array (first argument), starting from the front, until the predicate (second argument) returns true.

The alternative solution that you found at that other place does indeed solve the problem.

  • In this case the array is [2,1,3] and predicate is n > 1
  • 2 > 1 is true so we are done and we return the remaining array which is still [2,1,3]
  • In this case the array is [0, 1, 0, 1] and predicate is n === 1
  • 0 === 1 is false, so the entry is removed from the array and we have [1, 0, 1]
  • 1 === 1 is true so we are done and we return the remaining array which is still [1, 0, 1]

I hope this clears this up for you to some extent. Do let us know if you have any further questions.

Thanks, all my problems are solved once I read the problem again.