[Challenge] log("this") or log("this").withData({})

Keff - Aug 19 '20 - - Dev Community

This challenge is intended for JavaScript, but you can complete it with any language you like and can.


Fun little challenge. Hope you enjoy it 😄

Challenge Description

You need to create a function called log that receives a string and prints the string prepended with "log:". Easy.

Wait, log also returns an object with one method withData, which receives any data. If withData is called, it should print the message passed to log, prepended with "withData:" and appended with the data.

Check the following scenarios to understand the challenge better.

Scenario 1

If we call just log("test"); a message should be logged containing the message "log: test".

log("test"); // > log: test
Enter fullscreen mode Exit fullscreen mode

Scenario 2

But if we call the method withData as follows log("test").withData({ user: 'keff' });; It should log only the message "withData: test { user: 'keff' }".

See how the message from just log("test") is not printed. This is because the logic from log() is ignored when we call .withData().

log("test").withData(2); // > withData: test 2
Enter fullscreen mode Exit fullscreen mode

Can you accomplish this behavior?

💪 Best of luck! 💪


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .