JSFiddle - React, Tailwind, and code Playground

by davidhong

JavaScript

//
// Base Logger class used to log to default console
//

function Logger (source) {
    this.source = source;
}

['debug', 'info', 'error'].forEach(function (method) {
    var writer = this.call(console[method], console, '>>');
    Logger.prototype[method] = function () {
        var args = Array.prototype.slice.call(arguments);
        writer(args);
    }
}, Function.prototype.bind);


//
// TESTS
//

var logger = new Logger('JSFiddle α');

function add (x, y) {
    logger.debug(this, 'Add', x, y);
    return x + y;
}

function divide (x, y) {
    logger.debug(this, 'Divide', x, y);
    var z = parseInt(x / y, 10);
    if (isNaN(z) || z === Infinity || z === -Infinity) {
        logger.error('Cannot perform division', x, y, z);
        return NaN;
    }
    return z;
}
 
logger.info('-----------------');

logger.info('+ operations');
add(0, 1);
add(1^100, 2^300);

 logger.info('÷ operations');
divide(3, 2);
divide(2937, 293);
divide(-29, 0);

logger.info('-----------------');