Recursion challenge help

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.

This is my code:

let bool = true;
const callback = (num) => {
  if((num < 10) === false) {
    bool = false;
  };
}
const all = ([...arr]) => {
  if (arr.length === 0) {
    return bool;
  }
  return all(arr, callback(arr.pop()))
}

It works fine but there are a few issues.

  1. 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.

  2. 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.

  3. I want the callback function to be stored in a variable and not declared in the “all” function itself.

Please help. Thanks.

Is it requirement to use recursion here?
My first impulse when I saw your post was: ‘use Array.prototype.every()

And welcome to the forum!

EDIT. just to clarify: is the below - the behaviour we are trying to achieve in this challenge?

const myCallback1 = arrItem => arrItem > 10;

const myCallback2 = arrItem => arrItem < 20

const myArr1 = [6, 7, 8, 9]

const myArr2 = [22, 18, 17, 16]

console.log(all(myArr1, myCallback1))//false - at least one item in myArr1 < 10

console.log(all(myArr2, myCallback1))//true - every item in myArr2 > 10

console.log(all(myArr1, myCallback2))//true - every item in myArr1 < 20

console.log(all(myArr2, myCallback2))//false - at least one item in myArr2 > 20

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.

Ok, if recursion is part of the challenge, I think it would not be a spoiler to say, that something like this should work(if NOT use recursion):

const all = (arr, callback) => {
  return arr.every(callback);
}

Now I need a moment to think how to solve it with recursion :sweat_smile:

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. */

Recursion must be used to solve this challenge.

there should be icon with the pencil at the bottom of all your posts

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?

If you want to be able to access more than one argument passed to the all function, you will need to add additional parameter(s).

Also, if you add the “right” parameters, you do not need the global variable named bool.

Using a global variable like this a) isn’t recursion and b) prevents your function from ever being used twice

Thanks for the feedback! Do you have any recommendations for good practice questions on recursion?

Hmm… how would you rectify that?

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.

I would delete the global variable and use the return value of the recursive function call.