challenges

Home / Divisors

Divisors

Write a function the lists all the potential divisors of a number. You will likely need a loop to do this.

Function Scaffolding

/**
 * Generates a list of divisors of teh given number. 
 *
 * @param {number} num - The number to check.
 * @returns {array} A list of integer divisors.
 */
function getDivisors(num) {
    // ...
}

Possible Approach

  1. Create a function with one parameter. Have the function console.log() the input.
  2. Define an array named divisors.
  3. Create a loop that iterates n number of times.
  4. In each iteration check if the number is divisible by i.
  5. If the number is divisible by i, add it to the divisors array.
  6. Return the divisors array.

Additional Challenge

What is the lowest number of loop iterations needed to achieve this?

Assets

Download PDF Slide Desk