Script with Node.js not generating output

Asked

Viewed 106 times

0

Hello! I started to solve problems of the Urionlinejudge platform and in the first challenge I already had doubts about the solution with Node.js. The problem only asks to insert two integers (one in each row) and the script must return their sum in the requested format. The solution was accepted on the platform, however when I run the script on my terminal no output is ever generated. Why might this be happening? Follows the script:

var input = require('fs').readFileSync('/dev/stdin', 'utf8'); 
var lines = input.split('\n');

var a = parseInt(lines.shift()); 
var b = parseInt(lines.shift()); 
var x = a + b;

console.log('X = ' + x);

1 answer

1

All right, V. Rodrigues?

What’s happening is that when you run this on the platform, you enter the numbers and they’re available inside the directory /dev/stdin, on the server where the platform is running.

I’m no expert on OS issues, but as far as I know, /dev/stdin is a file that stores inputs (input data), so it asks you to type and consumes them from there.

To simulate this on your machine with this same code, you would need to insert 2 values under each other, in the file /dev/stdin somehow, you understand?

What you can also do is create a file and read it, which gives in it. See the example: https://repl.it/repls/DeadlySalmonTrust

A hug

  • Hey, Diogo. What’s up? Passing some file as the first argument of the readFileSync function the script works, but I could not yet make the script run just by inserting data into the terminal. I would like to make the above script work haha. Using the 'readline-Sync' library I was able to easily solve the problem. It is not possible to make changes to the stdin file, because it is a representation of the default input offered by the user, which in this case is the keyboard. Still the script doesn’t work. I find it very strange.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.