在hackerrank(node.js)上使用ProcessData包装函数了解如何运行我的代码时遇到一些麻烦。似乎我不能使用ProcessData包装器中的参数删除函数而不返回〜对stdout~没有响应。如何将他们的输入(一个整数字符串)传递给我的函数并获得某种响应,以便我可以调试?肯定会得到帮助。请参阅我的功能,编码环境,输入和输入。预期产量低于。谢谢!
供参考: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);
});
答案 0 :(得分:0)
您需要使用process.stdout.write
。例如:
function processData(input) {
//Enter your code here
process.stdout.write('5 1 2 3 4');
}
答案 1 :(得分:0)
代替使用
让输入= 4; // 4是整数变量
function processData(input){
//在输出整数之前,您需要使用toString()将其转换为字符串;
process.stdout.write(input.toString().trim());
}
//注意输出已转换为字符串
输出:4