dave dave - 6 months ago 34
Javascript Question

Is there a javascript function that reduces a fraction

say we have fraction 2/4, it can be reduced to 1/2. Is there javascript function that can do the reducing?

// Reduce a fraction by finding the Greatest Common Divisor and dividing by it.
function reduce(numerator,denominator){
  var gcd = function gcd(a,b){
    return b ? gcd(b, a%b) : a;
  gcd = gcd(numerator,denominator);
  return [numerator/gcd, denominator/gcd];

// [1,2]

// [463,117702]