Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 303
Kannan T Avatar asked Apr 20 '17 19:04

Kannan T


People also ask

How do I code JavaScript in HackerEarth?

To provide the solution, you need to do this: 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. }

Does HackerRank use JavaScript?

In HackerRank Tests, Questions based on HTML, CSS, or JavaScript are designed to assess the website coding and designing skills of Candidates.

Which is better for beginners HackerEarth or HackerRank?

Hackerrank is best place for #Beginners . Hackerrank.com and Hackerearth.com are by far the best. Hackerearth also has tutorials for the concepts/category of problems.

Which platform is better HackerRank or HackerEarth?

Here's why companies switch to HackerEarth from HackerRank Thanks to a premium library of over 13,000 coding questions, supremely easy user experience, and vastly superior customer support, many companies have switched over to HackerEarth from HackerRank.


1 Answers

Let's take a simple example from HackerEarth: https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/find-factorial/

To provide the solution, you need to do this:

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:

Here is how you could do it in hackerrank:

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

The difference seems to be that in HackerRank, you need to convert the output to string yourself. Hope it helps!

EDIT2:

For multiline input like:

5 1 1 2 3 4 1 

You can do this:

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

Notice that input is multi-line, so first you need to split it into lines by doing var data = input.split('\n');. Each split will give you string with spaces in between. So, to get individual characters, you have to split again but this time with space like var firstLine = data[0].split(' ');. Once you have all the input, you are left with writing your own algorithm. Notice that I have left comments too so that you know how to debug in the editor itself.

By the way this solution also works and is an accepted solution.

Hope this helps too!

like image 121
Pankaj Shukla Avatar answered Sep 20 '22 16:09

Pankaj Shukla