Can someone help me understand the purpose of Node.js code when using JavaScript on HackerRank?

I have a coding problem that will use HackerRank, which I am not familiar with. I started trying to get to know him before starting, and imagine my surprise when I saw the template code in this editor.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

The rest of the problems in HR have slightly modified versions of this, and I cannot help but wonder what really happens. It seems that there is some text file that the editor reads, and therefore can compare with my output?

I am very grateful for the understanding of this, since I am sure that I will have to write my own Node "template" when I make my coding call.

Thank!

+4
1

. , , .

// Begin reading from stdin so the process does not exit. (basically reading from the command line)
process.stdin.resume();
//set the enconding for received data to ascii so it will be readable
process.stdin.setEncoding('ascii');


//declare variables to process the data
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

//if data is coming through, put it in the input_stdin string. keep receiving data until no more comes through
process.stdin.on('data', function (data) {
    input_stdin += data;
});

//after the transmission when the end signal is received break the string up and push each new line (\n == new line) as an element into the array.
process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

//gives you one element per line to work with.
function readLine() {
    return input_stdin_array[input_currentline++];
}

( ), , .

, :

function processData(input) {
    //Enter your code here
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

, , , " ", - , .

. , , , , console.log() . , .

+2

Source: https://habr.com/ru/post/1658964/


All Articles