Thursday 20 April 2017

How to Use Javascript in Hackerrank and Hackerearth?

Hi am a newbie to competitive programming the only language i know is Javascript but if i select javascript option i couldn't even understand how to get input and how to print output in both the sites for some problems is Hackerrank the code looks like this

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

And in the same hackerrank for some problems the initial code looks like this

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

/////////////// ignore above this line ////////////////////

function main() {
  var n = parseInt(readLine());
}

Whereas in hackerearth the initial code look like this

   function main(input) {
        //Enter your code here
        process.stdout.write("Hello World!");
    }

    process.stdin.resume();
    process.stdin.setEncoding("utf-8");
    var stdin_input = "";

    process.stdin.on("data", function (input) {
        stdin_input += input;
    });

    process.stdin.on("end", function () {
       main(stdin_input);
    });

If someone give me an example of a program how to get the inputs and print output in those sites or any solved program of those sites using javascript also will do i guess.



via kannucodert14

No comments:

Post a Comment