Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

68
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!