This is the challenge: Write a function called all which accepts an array and a callback and returns true if every value in the array returns true when passed as parameter to the callback function.
I can’t put the callback function as an argument in the “all” function (for some reason) I’m hoping someone can give me insight as to why this is the case.
I tweaked it a little to be able to input an array of indefinite length. It’s just something I want this code to be able to do.
I want the callback function to be stored in a variable and not declared in the “all” function itself.
Glad to be here! It’s nice finally having somewhere to ask my questions.
Also, yes using recursion is a requirement and the examples you gave are pretty much accurate.
As long as all the elements in the array follow a set of rules, return true, else return false.
I don’t understand what is the purpose of declaring this global variable for solving this problem.
Can you please add one or more examples of calling all() function to your code?
And if this challenge is from some resource like Codewars or something like that, link to the source wouldn’t hurt, so others would have more context for helping you.
The global variable is my way of keeping track of the status of each element in the array.
Essentially if any element fails the condition, the variable changes to false meaning the entire array fails the condition.
I’ve tried editing the original post but I can’t find the edit button so I’ll just leave it here. I found the challenge on this website h ttp s:/ /www.codingame.com/playgrounds/5422/js-interview-prep-recursion
(Remove the spaces!)
It’s question 4 and this is the sample code given there:
var allAreLessThanSeven = all([1,2,9], function(num){
return num < 7;
});
console.log(allAreLessThanSeven); // false
Another example of the desired behaviour:
console.log(all([1, 4, 11, 40] , callback)) // Example 1
console.log(all([1, 4, 3, 5] , callback)) // Example 2
/* Example 1 should return false as not all elements in the array are less than 10.
Example 2 should return true as all elements in the array are less than 10
Notice callback is not passed as an argument in "all" in my original code?
I want to be able to pass it as an argument. */
Whoever created the challenge did not understand recursion is not really the best fit here. Recursion is better for solving problems where the number of iterations is not easily determined. Since only a one-dimension array is passed to the function, the length of it determines the number of iterations, therefore recursion is really unnecessary and makes the solution more complicated than needed.
Do you have an example of how exactly you tried to pass function as a second argument to the all function
And what errors do you get when trying to do that if any?
A couple of years ago, I went back through all the JavaScript algorithm problems (Basic, Intermediate, and Advanced) and rewrote all of them using recursion. Most did not need recursion and several were not as readable as my original iterative versions, but it really helped solidify recursion for me.