Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
Javascript find prime numbers in array wihhout using a function

Prime number is a number that is divided by itself or 1 but nothing else. Im trying to create a program that will ask the user for two numbers, and then find the prime numbers in the range between them without using a function and create a new array with only the prime numbers.

The first for loop is designated to contain the numbers between the start and end. The second loop is to check each number if it divided by any number from 2 up until the number tself (except itself and one).

Then, “if statement “ that state if even one number is divided by it without remainder: that its not an prime number and I should break this loop. This is where I get stuck.

I want somehow to tell the program that if it “broke” that means it isn’t a prime number, else.. it is!

if I used function i simply could use return and that will solve it, but I want without using function.

let start = +prompt("Give me a start number")
let end = +prompt("Give me an end number")
let newArray = []
let primeArray = []

for (let i = start; i <= end; i++) {
    newArray.push(i)
}

for (num of newArray) { 
    for (let i = 2; i < num; i++) { 
        if (num % i == 0 ) {
            break
        }
    }
    if (i !== num) {
        console.log(i)
        primeArray.push(num)
    } 
}
document.write(primeArray)

This is my solution:

let start = +prompt("Give me a start number")
let end = +prompt("Give me an end number")
let primeArray = []
let isPrime = true

if (start < 2)
{
    start = 2
}

for (num = start; num <= end; num++) 
{
    isPrime = true
    for (let i = 2; i < num; i++) 
    {
        if (num % i == 0) 
        {
            isPrime = false
            break
        }   
    }  
    if (isPrime == true) 
    {
        primeArray.push(num)
    } 
}

document.write(primeArray)
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I think I understand the problem - if break doesn't work, defining a boolean and flipping it to true would probably be a sufficient (dirty) fix for your code.

let start = +prompt("Give me a start number")
let end = +prompt("Give me an end number")
let newArray = []
let primeArray = []
let truthCheck =  true;

for (let i = start; i <= end; i++) {
    newArray.push(i)
}

for (num of newArray) { 
    for (let i = 2; i < num; i++) { 
        if (num % i == 0 ) {
            truthCheck = false;
        }
    }
    if (i !== num && truthCheck == true) {
        console.log(i)
        primeArray.push(num)
    } 
}
document.write(primeArray)
about 4 years ago · Juan Pablo Isaza Relatório

0

Problems:

  • The scope of i is incorrect. Because you used let inside of the loop initialization, it only exists until the bottom of the loop body. It does not exist when you are trying to check (i !== num). If you want to use it after the loop then you need to define it before the loop.
  • Your condition i !== num is inverted. That will be true only if num is not prime because you break out of the loop early when you discover a valid divisor > 2 (num % i == 0). Only if the loop completes (no divisors found) will i == num be true. Change the line to if (i == num) {.

The resulting fixed code:

let start = +prompt("Give me a start number")
let end = +prompt("Give me an end number")
let newArray = []
let primeArray = []

for (let i = start; i <= end; i++) {
    newArray.push(i)
}

for (num of newArray) {
    let i;

    for (i = 2; i < num; i++) {
        if (num % i == 0) {
            break
        }
    }

    if (i == num) {
        console.log(i)
        primeArray.push(num)
    }
}

document.write(primeArray)

Example: answering 562 and 850 will yield: [563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839]

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda