Recursive Approach
In this method, we will call the function recursively and return the output with the spread operator to get the array output.
Example:
function recursiveFactor(n, d) {
if (n < 1) return [];
if (n == 1) return [1];
if (n == 2) return [1,2];
if(n/d<2)
return [n];
if (n % d == 0) return [d, ...recursiveFactor(n, d + 1)];
return recursiveFactor(n, d + 1);
}
const num = 85;
console.log(
"All factors of " +
num + ": " +
recursiveFactor(num, 1)
);
Output
All factors of 85: 1,5,17,85
JavaScript Program to Find All Divisors of a Number
In this article, we will demonstrate different approaches to writing a JavaScript Program to Find all Divisors of a Number. We will have an input number and print all the divisors of that number in the form of a resultant array.