Created
March 10, 2021 12:23
-
-
Save mutoo/0c4c388749fedd22f16375c07f8cae40 to your computer and use it in GitHub Desktop.
This file contains 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 ratioConfig = (x, y) => ({ | |
x, | |
y, | |
ratio: x / y | |
}) | |
const commonRatio = [ratioConfig(1, 1), ratioConfig(4, 3), ratioConfig(16, 9), ratioConfig(16, 10)]; | |
const getRatioConfig = (x, y) => { | |
const ratio = x / y; | |
const rank = [...commonRatio].map((r) => ({ | |
...r, | |
delta: Math.abs(r.ratio - ratio), | |
})).sort((r1, r2) => r1.delta - r2.delta); | |
return ratioConfig(rank[0].x, rank[0].y); | |
} | |
const ret = getRatioConfig(982, 737); | |
console.log(ret.x, ret.y); // 4, 3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment