NODE.JS - Life, the Universe, and Everything problem

The code is running just fine over local environment. I’m unable to understand why it’s being continuously rejected over codechef.
Code is dead simple:

var stdin = process.openStdin();
stdin.addListener("data", function(d) {
var num = parseInt(d.toString());
if (num != 42)
  console.log(num);
else
  process.exit(0);
});

Have a look here: http://ideone.com/LAtQ4y

Your program runs once only. It outputs the 1st integer and then exits.

2 Likes

Thanks buddy for suggesting me to check on ideone. Now I’m using readline module for stdin & stdout and the code is working fine. Previous code was running just fine over my local environment. I still don’t understand why it was not working over ideone. If it’s possible will you please help me spot the error in previous code.

1 Like

I’m too late here, but input can be taken simply by

/*************************/
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
  // console.log(data)
});
/*************************/

Then the output can be shown as below

/*************************/
process.stdin.on('end', function() {
  // process data here
  // console.log(res)
});
/*************************/

You’d notice the 2nd callback doesn’t take any parameters.