How to use Javascript in Hackerrank and Hackerearth?

Hi, new to competitive programming, the only language I know is Javascript, but if I choose the javascript option, I would not even understand how to get input and how to print output on both sites, for some problems is Hackerrank, which 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 source 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());
}

While in hackerearth the source code is as follows

   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 gives me an example of a program on how to get the input and print the output on these sites or any permitted program of these sites using javascript, I also think.

+8
source share
3

HackerEarth: https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/find-factorial/

, :

function main(input) {
    //Enter your code here
    var num = parseInt(input, 10);//This line expects input to be a string so convert to an int as per problem
    var res=1;
    for(var i=num;i>1;i--) {
        res *= i; 
    }
    process.stdout.write(res);//This is how you write output.
} 

EDIT:

:

function main() {
    var n = parseInt(readLine());
    var strN = n.toString();//<-- Convert int n to string
    for(var i=1;i<=10;i++) {
        process.stdout.write(strN+" x "+i+" = "+n*i);//<-- formatting the 
                                                     //question requires
        process.stdout.write("\n");//<-- newline
    }
}

, , , HackerRank . , !

EDIT2

:

5 1
1 2 3 4 1

:

function main(input) {
    //Enter your code here
    var data = input.split('\n');
    var firstLine = data[0].split(' ');
    var len = firstLine[0];
    //process.stdout.write('length:'+len);
    var toFind = firstLine[1];
    //process.stdout.write('toFind:'+toFind);
    //process.stdout.write('\n');
    var arr = data[1].split(' '); 
    //process.stdout.write(arr);
    for(var i=len-1;i>=0;i--) {
        if(arr[i] == toFind){
            process.stdout.write(i+1);
            return;
        }
    }
    process.stdout.write(-1);
}

, , , var data = input.split('\n');. . , , , , var firstLine = data[0].split(' ');. , . , , , .

, .

, !

+14
0

, - JavaScript. , , , , !

0
source

Source: https://habr.com/ru/post/1675296/


All Articles