I am able to push the first integer element in the array. After that it takes every input values as a NaN.
How can we remove blank spaces in the array input at runtime. As Ryan said readLine is returning something that isn't parseable as an integer.
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());
var a = [];
for(var i=0;i<n;i++)
{
/*I am able to get first input but not for next iteration input value taken as NaN*/
var no = parseInt(readLine(),10);
console.log(readLine())
a.push(no);
}
}
For example,
input: 1 2 3 4 5
output: 1 NaN NaN NaN NaN
readLine()does?parseInt(readLine(), 10);readLine()readLineis returning something that isn't parseable as an integer.console.log(readLine())to find out what.input_stdin_array = input_stdin.split(" ");add this on yourprocess.stdin.on('end', function () {method instead of your statement it will solve your issue.