'kattis: why does my output contain my input?
Im trying to attempt a question on kattis Left beehind and my output is correct but somehow my input gets written with the output which im so confused about. Can anyone tell me what is the problem and how to get rid of the numbers in my output?
here is my code: (im using javascript)
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', (line) => {
var nums = line.split(' ');
var x = parseInt(nums[0]);
var y = parseInt(nums[1]);
if (x==0 && y==0 ) {
return;
}
if (x+y==13) {
console.log("Never speak again.")
}
else if (y>x) {
console.log("Left Beehind.")
}
else if ( x > y ) {
console.log ("To the convention.")
}
else{
console.log("Undecided.")
}
});
and this my output
17 3
To the convention.
13 14
Left Beehind.
8 5
Never speak again.
44 44
Undecided.
0 0
Solution 1:[1]
Your input is being written with your output because you specified process.stdout, which is your NodeJS console, as an output for the readline interface.
So, readline is successfully reading the lines, emitting the line event for each line it receives/finds, then you are performing some logic and printing some text using console.log (which in turn uses process.stdout to print to the NodeJS console). Finally, readline is streaming the lines to the same output.
Remove the output option and it should work as expected.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ernesto Stifano |
