简体   繁体   English

有人帮我理解在HackerRank上使用JavaScript时Node.js代码的用途吗?

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

I have a coding challenge coming up that will be using HackerRank, which I am unfamiliar with. 我有一个编码挑战即将使用HackerRank,我不熟悉。 I started trying to get familiar with it before I started and imagine my surprise when I saw this boilerplate-ish code in the 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 challenges on HR have slightly modified versions of this and I can't help but wonder what is really going on. 人力资源面临的其他挑战略有修改,我不禁想知道究竟发生了什么。 It seems like there is some sort of text file that the editor is reading from and is therefore able to compare to my output? 看起来编辑器正在读取某种文本文件,因此能够与我的输出进行比较?

I'd really appreciate any insight into this as I am pretty sure that I will have to write my own Node "boilerplate" when I do my coding challenge. 我非常感谢对此的任何见解,因为我很确定在编写我的编码时我必须编写自己的Node“样板”。

Thanks! 谢谢!

The code basically receives the information you need as input for the challenges. 代码基本上接收您需要的信息作为挑战的输入。 This particular one makes it so that the input comes through the same way the describe it in the challenge. 这个特定的一个使得输入通过与挑战中描述它相同的方式来实现。

// 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++];
}

Usually this code is followed by some more (below the comment line) where you already get variables assigned to have the data in a workable format. 通常,此代码后面会有更多(在注释行下方),您已经获得了变量,以便将数据保存为可行的格式。

There is another version of the code that doesn't deliver as bite sized chunks: 还有另一个版本的代码没有提供一口大小的块:

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);
});

As you can see, the code is basically the same but some of the boiler plate 'making the input usable' is something you have to do in this one. 正如您所看到的,代码基本相同,但是一些“使输入可用”的锅炉板是您必须要做的。

Just don't be confused. 只是不要混淆。 Every challenge I solve there, the first thing I do is console.log(input) or any other pre-created variable. 我在那里解决的每一个挑战,我做的第一件事是console.log(输入)或任何其他预先创建的变量。 it helps me knowing what is actually where. 它帮助我知道实际上在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM