Skip to content

Instantly share code, notes, and snippets.

@bittu
Created February 21, 2023 16:35
Show Gist options
  • Save bittu/c848e225723dc4016501dfa7f33338a5 to your computer and use it in GitHub Desktop.
Save bittu/c848e225723dc4016501dfa7f33338a5 to your computer and use it in GitHub Desktop.
Find aspect ratio of any resolution (width x height)
function gcd(a, b) {
return (b === 0) ? a : gcd(b, a % b);
}
function getAspectRatio(width, height) {
const r = gcd(width, height);
return `${width / r} : ${height / r}`
/*
return {
x: width / r,
y: height / r
}
*/
}
console.log(getAspectRatio(1920, 1080)); // "16 : 9"
console.log(getAspectRatio(1632, 510)); // "16 : 5"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment