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

70
Visualizações
Function that returns the factors of a number into an empty array

I have an assignment that requires me to find if the sum of the factors of a number minus the number itself is equal to the number. I have written the function below but does not return the correct factors. Please help!

function findNumbers(numbers) {
  let factorNumbers = [];
    
  for(i = 1; i < numbers; i++) {
    if(numbers % i === 0) {
      return factorNumbers.push(i);
    }
  };
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You just pushed the factors in an array. Firstly, you have to add them up and also call the function and console log the result or maybe write an if statement to verify whether this number is a perfect number or not.

function findNumbers(number)
    {
    let result = 0;

       for(let i=1; i<=number/2; i++)
         {
             if(number%i === 0)
              {
                result += i;
              }
         }
      console.log(result);
     } 
    findNumbers(6); 
about 4 years ago · Juan Pablo Isaza Relatório

0

You need to check each number up to and equaling the number you pass in to the function, and then return the array after the iteration is complete.

function findFactors(number) {
  const factorNumbers = [];
  for (let i = 1; i <= number; i++) {
    if (number % i === 0) {
      factorNumbers.push(i);
    }
  }
  return factorNumbers;
}

console.log(findFactors(12));
console.log(findFactors(30));

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