简体   繁体   English

ProcessData Node.js stin / stdout)问题

[英]ProcessData Node.js stin/stdout) issues

Having a bit of trouble understanding how to run my code using a ProcessData wrapping function on hackerrank (node.js). 在理解如何使用hackerrank(node.js)上的ProcessData包装函数运行代码时遇到一些麻烦。 It seems I can't drop a function with the parameters I have in the ProcessData wrapper without getting back ~no response on stdout~. 似乎我不能在没有返回〜对stdout无响应〜的情况下,删除带有ProcessData包装器中参数的函数。 How do I pass their input (a string of integers) in to my function and get a response of some sort so I can debug? 如何将它们的输入(整数字符串)传递给函数,并获得某种响应,以便进行调试? Help would definitely be appreciated. 帮助将不胜感激。 Please see my function, the coding environment, the input & expected output below. 请在下面查看我的功能,编码环境,输入和预期输出。 Thanks! 谢谢!

For reference: https://www.hackerrank.com/challenges/array-left-rotation 供参考: https : //www.hackerrank.com/challenges/array-left-rotation

    /*

     Input Format

    The first line contains two space-separated integers 
denoting the respective values of  (the number of integers) 
and  (the number of left rotations you must perform). 
    The second line contains  space-separated integers 
describing the respective elements of the array's initial state.

    */

/*Environment with my code, below (how do I pass input as an argument to this function?? 
console.log or return the data, and get a response for debugging?)*/

function processData(input) {
    //Enter your code here
    var rotate = function(num,turns,ar){
    var store = ar.slice(0, turns);
    for(var i=0; i<store.length; i++){
        ar.shift();
        ar.push(store[i]);
    }
    return ar;
};
} 

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

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

You need use process.stdout.write . 您需要使用process.stdout.write For example: 例如:

function processData(input) {
    //Enter your code here
    process.stdout.write('5 1 2 3 4');
} 

instead use 代替使用
let input = 4; 让输入= 4; //4 is an integer variable // 4是一个整数变量

function processData(input) { 函数processData(input){

//before outputing the integre you need to convert it to string using toString(); //在输出整数之前,您需要使用toString()将其转换为字符串;

process.stdout.write(input.toString().trim());

} }


//Note the output is converted to string //注意输出已转换为字符串
Output: 4 输出4

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

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