Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

273
Vistas
Cant' find out the cause of infinite loop in Javascript (After translating a function from Python)

I'm translating a function I've wrote in Python to Javascript but In JS I have an infinite loop while in Python everything works fine.

Here's my code in Python:


def isPrime(n: int) -> bool:

    # Since neither 1 nor 0 is a prime number, return false
    if n == 1 or n == 0:
        return False

    # Loops from 2 to n
    for i in range(2, n):

        # If the number is divisible by i, then n is not a prime number
        if n % i == 0:
            return False

    # Otherwise, n is a prime number
    return True

def getPrimes(n: int) -> list[int]:
    """
    Take a positive int number n as a parameter and prints to the console the first n prime numbers
    """

    primes = []
    count = 0

    while len(primes) < n:
        if isPrime(count):
            primes.append(count)
        count += 1

    return primes

Here's my code in Javascript:


function isPrime(num) {

    // Since neither 1 nor 0 is a prime number, return false
    if (num == 1 || num == 0) {
        return false
    }

    // Loops from 2 to n
    for (let i = 2; i <= num; i++) {
        
        // If the number is divisible by i, then n is not a prime number
        if (num % i == 0) {
            return false
        }
    }

    // Otherwise, n is a prime number
    return true
}

function getPrimes(num) {

    let primes = [];
    count = 0;

    while (primes.length < num) {
        if (isPrime(count) === true) {
            primes.push(count);
        }
        count++;
    }

    return primes;
}

In Python, when I call the function getPrimes(n) it correctly returns an array containing the first n prime numbers.

But in Javascript, the same function causes an infinite loop.

Why is that happening? Thank you in advance.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's not causing infinite loop, your isPrime(num) function just returns false for every number, because you are having i <= num, which causes num % num == 0 evaluating to true and returning false. Just fix it to i < num

function isPrime(num) {

    // Since neither 1 nor 0 is a prime number, return false
    if (num === 1 || num === 0) {
        return false
    }

    // Loops from 2 to n
    for (let i = 2; i < num; i++) {
        
        // If the number is divisible by i, then n is not a prime number
        if (num % i === 0) {
            return false
        }
    }

    // Otherwise, n is a prime number
    return true
}

function getPrimes(num) {

    let primes = [];
    count = 0;

    while (primes.length < num) {
        if (isPrime(count) === true) {
            primes.push(count);
        }
        count++;
    }

    return primes;
}


console.log(getPrimes(10))  // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda