Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
89
rated 0 times [  96] [ 7]  / answers: 1 / hits: 22009  / 14 Years ago, mon, january 10, 2011, 12:00:00

say we have fraction 2/4, it can be reduced to 1/2.



Is there a JavaScript function that can do the reducing?


More From » function

 Answers
37
// 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]

[#94287] Sunday, January 9, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ignacio

Total Points: 467
Total Questions: 128
Total Answers: 79

Location: Luxembourg
Member since Tue, Mar 14, 2023
1 Year ago
ignacio questions
;