javascript aspect ratio calculation (with GCD)

Source: https://gist.github.com/sinky/8351745

by sinky

JavaScript

/* GCD */
function gcd (a, b) { return (b == 0) ? a : gcd (b, a%b); } // Source: http://stackoverflow.com/a/1186465

/* ratio is to get the gcd and divide each component by the gcd, then return a string with the typical colon-separated value */
function ratio(x,y) {c=gcd(x,y); return ""+(x/c)+":"+(y/c)}


bodyLog('ratio(1920,1080)', ratio(1920,1080));
bodyLog('ratio(320,240)', ratio(320,240));
bodyLog('ratio(360,240)', ratio(360,240));

function bodyLog(a,b) {
    $("body").append('<div>'+a+'<br />'+b+'</div>');
}