compile | Cell 1 | Cell 3 | Search

The logger function logs a message to the console and returns a closure that can be used to print additional messages, but with a prefix of "wtf? ". The test function demonstrates the usage of logger by calling it and logging "wtf? undefined" to the console.

Cell 2

var logger = (message) => {
    console.log(message)
    return (m) => 'wtf? ' + m
}

function test() {
    return logger `test` `another`;
}

test()

What the code could have been:

/**
 * A utility function that logs a message to the console and returns a callback.
 * The callback function returns a prefixed message.
 * @param {string} message - The message to be logged and returned.
 * @returns {function} A callback function that returns a prefixed message.
 */
function logger(message) {
    // Log the message to the console
    console.log(message);
    
    // Return a callback function that prefixes the input message
    return (m) => `wtf? ${m}`;
}

/**
 * A function that tests the logger utility.
 * @returns {void}
 */
function test() {
    // Call the logger function with multiple messages
    const loggerInstance = logger('test')('another');
    
    // Log the result of the logger instance
    console.log(loggerInstance);
}

// Call the test function
test();

Code Breakdown

logger Function

test Function

Usage