JSFiddle - React, Tailwind, and code Playground

by krofdrakula

JavaScript

function getClosestRatio(w, h) {
    var ratios = { "4:3": 4/3, "16:9": 16/9 }, d = 10, q = w / h, closest = "", c;
    for(var r in ratios) if(ratios.hasOwnProperty(r)) {
        c = Math.abs(q - ratios[r]);
        if(d > c) {
            closest = r; d = c;
        }
    }
    return closest;
};

$('body').append('360/240 is closest to ratio ' + getClosestRatio(360,240));