I'm struggling with an exercise in the latest edition of a book, specifically in Chapter 5 which covers Higher-Order Functions. The prompt for the exercise is as follows:
"Similar to the some method, arrays also contain an every method. This method returns true when a given function returns true for every element in the array. You can think of some as the equivalent of the || operator for arrays, and every as the equivalent of the && operator."
The task is to create an every function that takes an array and a predicate function as arguments. Two versions are required: one using a loop and the other utilizing the some method."
In the provided code sandbox, the function template looks like this:
function every(array, test) {
// Your code here.
}
Sample test cases are shown below:
console.log(every([1, 3, 5], n => n < 10));
// → true
console.log(every([2, 4, 16], n => n < 10));
// → false
console.log(every([], n => n < 10));
// → true
As I review the test cases, I'm struggling to understand how to pass the array parameter to the test parameter, especially considering the test parameter can be any function. The instructions simply mention "a predicate function." While I could continue using n as the variable, it seems like the author wants me to create my own array.prototype.every() method.
Can anyone provide insight into what this exercise is aiming for? Or perhaps point out something I may be overlooking?