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

69
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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