Joeb Rogers
Joeb Rogers

Reputation: 348

Prime Factor Calculator JS - Can't find infinite loop

So I was trying to go back over my problem solving abilities and wanted to redo my prime factor calculator. Code refactoring, more efficient, etc as I was a beginner at JS when I made it.

In recreating it I have come across a rather large issue - an infinite loop. Now, I've broken down my function into different parts and called them separately - they work fine. The main function itself even works fine, so long as the number is 10 or less. But for some reason whenever I call the function with a parameter greater than 10 there is an infinite loop.

I'm sorry if the answer is glaringly obvious, it's quite late at night. I just can't seem to spot it.

The plain code is here:

var findPrimeFactors = function (number) {

    var isPrime = function (number) {
        var primes = [];
        for (i = 2; i < number; i++) {
            if (number % i === 0) {
                return false;
            }
        }
        primes.push(number);
        return primes;
    };

    var findFactors = function (number) {
        var factors = [];
        for (i = 2; i < number; i++) {
            if (number % i === 0) {
                factors.push(i);
            }
        }
        return factors;
    };

    var factors = findFactors(number);
    var primes = [];
    for (i = 0; i < factors.length; i++) {
        primes += isPrime(factors[i]);
    }
    return primes;
};

console.log(findPrimeFactors(10));

The fiddle for the code is here: https://jsfiddle.net/uk26q4ff/

Thanks everyone!

Upvotes: 1

Views: 188

Answers (2)

Olivier De Meulder
Olivier De Meulder

Reputation: 2501

I found a couple of bugs.

  1. Your isPrime function should return either true or false, not an array
  2. the loop in findFactors should include the 'number' value (changed < to <=)
  3. The infinite loops was cause by using the same variable i in every loop.
  4. Finally I changed the following I changed the following line at the end: if (isPrime(factors[k])) primes.push(factors[k]);

Here is how I would do it:

var findPrimeFactors = function (number) {

    var isPrime = function (number) {
        var primes = [];
        for (i = 2; i < number; i++) {
            if (number % i === 0) {
                return false;
            }
        }
        //primes.push(number);
        //return primes;
        return true;
    };

    var findFactors = function (number) {
        var factors = [];
        for (j = 2; j <= number; j++) {
            if (number % j === 0) {
                factors.push(j);
            }
        }
        return factors;
    };

    var factors = findFactors(number);
    var primes = [];
    for (k = 0; k < factors.length; k++) {
        //primes += isPrime(factors[k]);
        if (isPrime(factors[k])) primes.push(factors[k]);
    }
    return primes;
};

console.log(findPrimeFactors(37));

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 191037

Likely you are hitting this because in each function you aren't declaring i so it is using it from the global scope.

Upvotes: 2

Related Questions