I always find myself needing to know how numbers compare. But I hate taking out a calculator or grabbing pencil and paper to work it out. So I wrote a small script to do it for me.

**Method**

Math.Ratio = function (rt1, rt2) { var rt1n = rt1.split(':'); var rt2n = rt2.split(':'); var rntd1i = false, rntd2i = false; for (var i = 0; i < rt1n.length; i++) { if (rt1n[i] == '?') { rntd1i = i; } } for (var ii = 0; ii < rt2n.length; ii++) { if (rt2n[ii] == '?') { rntd2i = ii; } } function rtDetermine(r1, r2, ri) { var isum = r1[0] / r1[1]; return ri == 0 ? r2[1] * isum : r2[0] / isum; } if (rntd1i !== false && rntd2i !== false) { console.warn('Whoops, can only work with one unknown number.'); return false; } else if (rntd1i !== false) { return rtDetermine(rt2n, rt1n, rntd1i); } else if (rntd2i !== false) { return rtDetermine(rt1n, rt2n, rntd2i); } else { console.warn('Whoops, need at least one unknown number.'); return false; } };

And then you can call it like so:

Math.Ratio('1920:1080','?:720');//1280

**Demo**

? | |

## Comments

## Post a Comment