简体   繁体   中英

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). It seems I can't drop a function with the parameters I have in the ProcessData wrapper without getting back ~no response on stdout~. 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

    /*

     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 . For example:

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

instead use
let input = 4; //4 is an integer variable

function processData(input) {

//before outputing the integre you need to convert it to string using toString();

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

}


//Note the output is converted to string
Output: 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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