Joined Logger - JavaScript (Basic) Certification Test Solution | HackerRank




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.
  • The function f uses the logger to write joined textvalues of messages that have a level value greater than or equal to the level parameter.
  • 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 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.

Implementation of the function will be tested by a provided code stub on several input files. Each input contains parameters for joined Logger, followed by several value to construct messages to log. First, the joined Logger function will be called with the given parameters. Then, the returned function will be called with all the messages constructed from the values given in the input. The result of the latter call will be printed to the standard output by the provided code.

$ads={2}


Solution:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
function joinedLogger(level, sep) {
    var s=[]
    return function(...msg)
    {
        msg.forEach(arg => {
            if(arg.level>=level)
            {
                s.push(arg.text)
            }
        });
        var ans=String(s.join(sep))
        ws.write(ans)
    
    }
  // write your code here
}
Labels : #hackerrank ,#hackerrank certification ,#javascript (basic) ,

Post a Comment