This one’s in C# but almost same syntax would apply in javascript:

public static int GreatestCommonDenominator(int a, int b) { while (true) { if (b == 0) return a; var a1 = a; a = Math.Min(a, b); b = Math.Max(a1, b) % Math.Min(a1, b); } }

What the buckets does it do? Gives you the greatest common denominator so that if you’re trying to provide viewers with a ratio, e.g 2:3, and you’re starting with numbers like 180:270. One note: It expects integers only.

So what would you do if the ratio is 181:270, which won’t reduce? You could add a few rules, depending on how important it is to be exact vs. approximate. For example, use a string function to get the length of the numbers, divide them by 10, round the results and then do the comparison. If still no change, repeat.