JSFiddle - React, Tailwind, and code Playground

by cvoll

JavaScript

var fizzBuzz = function (start, end) {
    var response = [];
    for (var i = start; i < end + 1; i++) {
        var output = '';
        if (i % 3 == 0) output  = 'fizz';
        if (i % 5 == 0) output += 'buzz';
        response.push(output || i);
    }
    return response;
}

var TestSuite = function(tests) {
    var errors = {
            E_EQUAL : 'not equal',
            E_TRUE  : 'not true',
            E_FALSE : 'not false',
            E_EXCEPT: 'exception'
        },
        styles = {
            PASS: 'color: #0F9D58',
            FAIL: 'color: #FF1919; font-weight: 600'
        },
        total = 0,
        passed = 0,
        currentTest;
    
    return {
        run: function () {
            this.setup();
            for (i in tests) {
                currentTest = i;
                try {
                    tests[i].call(this);
                } catch(e) {
                    this.fail(errors.E_EXCEPT, e);
                }
            }
            this.finish();
        },
        
        setup: function () {
            total = 0;
            passed = 0;
            console.group('Running tests...');
        },
        
        finish: function () {
            var style = passed == total ? 'PASS' : 'FAIL';
            console.groupEnd();
            console.log('%c' + passed + '/' + total + ' passed', styles[style]);
        },
            
        assertEqual: function (a, b) {
            return this.assert(a.toString() == b.toString(), errors.E_EQUAL, arguments);
        },
        
        assertTrue: function (condition) {
            return this.assert(!!condition, errors.E_TRUE, arguments);
        },
        
        assertFalse: function (condition) {
            return this.assert(!condition, errors.E_FALSE, arguments);
        },
        
        assert: function (condition, err, args) {
            return condition ? this.pass() : this.fail(err, args);
        },
        
        pass: function () {
            total++;
      ...