I don’t understand a couple of things.
- why the instructions say the predicate function takes element, index, and array when the predicate function in the drop-while.js only takes two arguments.
from the drop-while,js:
const indexIsSmallerThanElement = (element, index) => index < element;
I see other people saying they did something like this in their solutions:
let dropNumber = array.findIndex(function(element, index){
!(predicate(element, index, array))
is the idea that some predicate functions might need the whole array (and be defined with 3 parameters) so we are passing it just in case? Or am I missing something.
My code is different and omits the array argument, but still works:
dropWhile(arr, predicate) {
for(i = 0; i< arr.length; i++) {
if(!predicate(arr[i], i )) {
this.drop(arr, i);
}
}
return arr;
- I don’t understand how element, index are being passed in the common solution I pasted above. They are nowhere defined that I can see. When I pass arr[i] and [i] I know I’m passing the value of the element and the index to the predicate. How does JS compiler know this is the intention from the code snippet:
array.findIndex(function(element, index){
!(predicate(element, index, array))
I’m honestly totally confused about this. I haven’t the first idea how the compiler knows what element and index are.
would appreciate any help.