JSFiddle - React, Tailwind, and code Playground

by jfriend00

HTML

<div id="output"></div>

JavaScript

function out(str) {
    var el = document.getElementById("output");
    var div = document.createElement("div");
    div.innerHTML = str;
    el.appendChild(div);
}

// from Doug Crockford http://javascript.crockford.com/remedial.html
function typeOf(value) {
    var s = typeof value;
    if (s === 'object') {
        if (value) {
            if (Object.prototype.toString.call(value) == '[object Array]') {
                s = 'array';
            }
        } else {
            s = 'null';
        }
    }
    return s;
}

function checkTypes(argList, typeList) {
    for (var i = 0; i < typeList.length; i++) {
        out(typeOf(argList[i]) + ", " + typeList[i]);
        if (typeOf(argList[i]) !== typeList[i]) {
            throw 'wrong type: expecting ' + typeList[i] + ", found " + typeOf(argList[i]);
        }
    }
}
                   
function test(a, b, c) {
    checkTypes(arguments, ["array", "object", "number"]);
    out("finished type checking - no errors<br><br>");
}

try {
    test([1,2,3], {test: "foo"}, 4);
    test(3, {test: "foo"}, 4);
} catch(e) {
    out("***" + e);
}