challenges

Home / Common DenominAtors

Common Denomintors

Write a function that calculates the greatest common denominator of the first and second parameters.

Function Scaffolding

/**
 * Calculates the greatest common denominator (GCD) of two numbers.
 *
 * @param {number} a - The first number.
 * @param {number} b - The second number.
 * @returns {number} The greatest common denominator of a and b.
 */
function getGCD(a, b) {
    // ...
}

Possible Approach

  1. Create a function with one parameter. Have the function console.log() the input.
  2. Determine which variable is smaller.
  3. Create a loop that iterates through the smaller value starting with the number down to one.
  4. Write an if statement that check if both numbers are divisible by the counter.
  5. If both are divisible, return the counter.

Assets

Download PDF Slide Desk