Created
February 12, 2019 22:57
-
-
Save michaelwclark/391c19d01c1b0ab6c3d59f298d6f08f4 to your computer and use it in GitHub Desktop.
gets the aspect ratio. Leaves a bit to be wanted.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const aspectRatio = (height, width, seperator=':')=> { | |
const gcd = (a, b) => b === 0 ? a : gcd(b, a % b) | |
const [h1, w1] = height < width ? [width, height] : [height, width] | |
const divisor = gcd(h1, w1) | |
return `${h1/divisor}${seperator}${w1/divisor}` | |
} | |
console.log(aspectRatio(549,500)) // 549:500 | |
console.log(aspectRatio(1024,768)) // 4:3 | |
console.log(aspectRatio(533,320)) //533:320 | |
console.log(aspectRatio(1106,2048)) // 1024:553 | |
console.log(aspectRatio(100,100)) // 1:1 | |
console.log(aspectRatio(533,320)) //533:320 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment