JSFiddle - React, Tailwind, and code Playground

by Erik East

HTML

<h2>Module 08</h2>
<h3>Assignment 9</h3>
<p>Please insert a real number (0-6): <input type='text' id='number' style="width: 20" onkeypress='captureEnter(event);'></p>
<p id="demo"></p>

JavaScript

var rules = [0.00, 0.04, 0.11, 0.60, 0.87, 0.95, 1.00];

    function captureEnter(e){
        if(e.keyCode === 13){
            supremeAlgorithm(document.getElementById('number').value);
        }
        return false;
    }

    var supremeAlgorithm;
    supremeAlgorithm = function (x) {

        var xa = Math.floor(x);
        var xb = Math.ceil(x);

        var y = 0; //output
        var ya = rules[xa];
        var yb = rules[xb];

        if (typeof rules[x] == 'undefined') {
            var xMath = (parseFloat(x) - parseFloat(xa)) / (parseFloat(xb) - parseFloat(xa));
            var yMath = (parseFloat(yb) - parseFloat(ya));
            y = parseFloat(ya) + parseFloat(yMath) * parseFloat(xMath);
        }else{
            y = rules[x];
        }
        document.getElementById('number').innerHTML = '';
        document.getElementById('demo').innerHTML = parseFloat(y).toFixed(2);

    };