Home

Division

Resources

  1. CoderByte

Question

Have the function Division(num1,num2) take both parameters being passed and return the Greatest Common Factor. That is, return the greatest number that evenly goes into both numbers with no remainder. For example: 12 and 16 both are divisible by 1, 2, and 4 so the output should be 4. The range for both parameters will be from 1 to 10^3.

Use the Parameter Testing feature in the box below to test your code with different arguments.

Answer

function Division(num1, num2) { const max = Math.max(num1, num2); const min = Math.min(num1, num2); if (max % min === 0) { return min; } let res = 1; for (let i = 0; i <= min / 2; i++) { if (max % i === 0 && min % i === 0) { res = i; } } return res; }