JavaScript (Basic)

#1 JavaScript: Joined Logger

In this challenge, each message object has two properties:

  • property level having an integer value

  • property text having a string value

For example: msg = { level: 2, text: "foo" }

There is an implementation of a simple logger function provided that:

  • takes a message object as an argument

  • writes the text of the message to the defined output.

Implement a function joinedLogger that:

  1. takes two arguments: integer level and string separator.

  2. returns a new function, f, such that f takes a variable number of message objects.

    1. The function f uses the logger to write joined text values of messages that have a level value greater than or equal to the level parameter.

    2. The text values must be joined by the separator string parameter, in the order they are passed to the function.

For example, let's say there are 3 defined messages: msg1 = { level: 10, text: "foo" } msg2 = { level: 20, text: "bar" } msg3 = { level: 30, text: "baz" }

Calling joinedLogger(15,';') must return a function f, such that calling f(msg1, msg2, msg3) causes the logger to write the string "bar,baz" to the defined output. The level passed to joinedLogger is 15, and the separator is ';'. Only msg2 and msg3 have a level greater than or equal to 15, so the text of those messages, "bar" and "baz", is joined with the ';' separator and written to the defined output by the logger.

function joinedLogger(level, sep) {
    // write your code here
    return function f(...messages) {
        let str = "";
        messages.forEach(msg => {
            if (msg.level >= level) {
                str = str + msg.text + sep;
            }
        })
        let len = sep.length;
        let strArray = str.split("");
        strArray.splice(strArray.length - len, len);
        str = strArray.join("");
        console.log({ str, messages, level, sep });
        fs.createWriteStream(process.env.OUTPUT_PATH).write(str);
        return;
    }
}

#2 JavaScript: Step Counter

In this challenge, you are provided with the implementation of a simple counter object:

const counter = (function counter() {
    let value = 0;
    return {
        getValue: function () {
            return value;
        },
        changeBy: function (k) {
            value += k;
        },
    }
})(); 

Your task is to implement a function stepCounter that:

  • takes a single parameter k

  • returns a new object, representing a step counter with the initial value 0 and with three methods:

    • increment(): increments the current value by k

    • decrement(): decrements the current value by k

    • getValue(): returns the current value

Your implementation must encapsulate the provided counter object and use it for its implementation. The object returned by stepCounter most not have a changeBy property.

function getFixedCounter(k) {
    // write your code here
    let myCounter = counter;
    return {
        increment: () => {
            myCounter.changeBy(k);
        },
        decrement: () => {
            myCounter.changeBy(-k);
        },
        getValue: () => {
            return myCounter.getValue();
        }
    }
}

Last updated

Was this helpful?