dave - 1 year ago 72

Javascript Question

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

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

```
// 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];
}
reduce(2,4);
// [1,2]
reduce(13427,3413358);
// [463,117702]
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**