Friday, 2 June 2017

Processing of integers from user input in NodeJS

I'm working on the Theatre Square problem from Codeforces. I've already written a script which is perfectly working in the browser, and now I'm struggling with NodeJS :)

Here's my code:

const readline = require('readline');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

rl.question("Enter three positive integers", function(userInput) {

const input = userInput.toString().split('');
n = input[0];
m = input[2];
a = input[4];

if (n >= 1 && m >= 1 && a >= 1 && n <= Math.pow(10, 9) && m <= Math.pow(10, 9) && a <= Math.pow(10, 9)) {
    console.log(Math.ceil((n * m) / (a * a)));
}
    else console.log("Enter three integers between 1 and 10^9");

rl.close();
});

After I run this script and enter the three integers, the console always responds with "Enter three integers between 1 and 10^9".

I tried removing the condition and leaving just console.log(Math.ceil((n * m) / (a * a))); - in this case, the response is NaN, so I'm guessing my mistake must be here somewhere:

rl.question("Enter three positive integers", function(userInput) {

const input = userInput.toString().split('');
n = input[0];
m = input[2];
a = input[4];

It's my very first code in NodeJS. Any help will be very appreciated!:)



via Athena

No comments:

Post a Comment