I have encountered an issue with a simple JavaScript function I created to factor down numbers into primes. The problem arises when the input number is the product of duplicate prime numbers, as the function does not include these duplicates in the array of factors. Let's take the number 28 as an example. 28 can be expressed as 2 * 2 * 7 = 2^2 * 7. When I run my function factor(n) with n = 28, I expect the result to be [2, 2, 7]. However, the current output is only [2, 7]. Can anyone assist me with fixing this problem? Below is the code for the function in JavaScript:
function factor(n) {
var factors = [];
for (var i = 2; i < n; i++) {
if (divisible(n,i)) {
if (isPrime(i)) {
factors.push(i);
}
factor(i);
}
}
console.log(factors);
}
Thank you in advance!