Skip to content

Instantly share code, notes, and snippets.

@fhefh2015
Forked from mutoo/get-common-ratio.js
Created March 11, 2021 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fhefh2015/b485cfc06011951cbfd44026d1c0e0f4 to your computer and use it in GitHub Desktop.
Save fhefh2015/b485cfc06011951cbfd44026d1c0e0f4 to your computer and use it in GitHub Desktop.
{
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