JSFiddle - React, Tailwind, and code Playground

by timrwood

CSS

code {
    display:block;
}

JavaScript

function log (m) {
    var t = document.createElement('code');
    t.textContent = m;
    document.body.appendChild(t);
}

function fraction (f) {
    var d, n,
        bestd,
        bestn,
        score,
        bestScore = 1;
    
    for (d = 2; d < 11; d++) {
        for (n = 1; n < d; n++) {
            score = Math.abs((n / d) - f);
            if (score < bestScore) {
                bestd = d;
                bestn = n;
                bestScore = score;
            }
        }
    }
    
    return bestn + "/" + bestd;
}

for (var d = 1; d < 11; d++) {
    for (var n = 1; n < d; n++) {
        log(n + '/' + d + ' ' + fraction(n/d));
    }
}