How to get Input from stdin / input.txt in Node.js / JavaScript↵
==================↵
↵
###To use in Codeforces↵
↵
`readline()` Reads one line from stdin.↵
↵
to get some space separated values from a line:↵
↵
```↵
var num = readline().split(" ").map(x => parseInt(x)); // num will be an array [1,2,3]↵
var x = num[0]; // to store values in different variables. ↵
var y= num[1];↵
var z= num[2];↵
↵
↵
var num2= readline(); // reads next line.↵
```↵
####For Output↵
```↵
print(x);↵
```↵
↵
####Refer [this submission](https://codeforces.com/contest/1199/submission/58184695) for a sample program submission.↵
↵
↵
↵
↵
###To run your programs locally.↵
`readline()` do not work by default if you try to use it locally on your computer.↵
↵
#### Write this header code in the beginning of your js code↵
↵
```↵
'use strict';↵
↵
process.stdin.resume();↵
process.stdin.setEncoding('utf-8');↵
↵
let inputString = '';↵
let currentLine = 0;↵
↵
process.stdin.on('data', inputStdin => {↵
inputString += inputStdin;↵
});↵
↵
process.stdin.on('end', _ => {↵
inputString = inputString.trim().split('\n').map(string => {↵
return string.trim();↵
});↵
↵
main(); ↵
});↵
↵
function readline() {↵
return inputString[currentLine++];↵
}↵
↵
// thats all what you have to write to get input from stdin, using readLine.↵
↵
↵
// Main code runs in main();↵
↵
↵
function main() {↵
const parameterVariable = readline();↵
constvar line2 = readline(); // you cannot use let on CF so use just var↵
↵
greeting(parameterVariable);↵
greeting(line2);↵
}↵
↵
function greeting(parameterVariable) {↵
console.log('Hello, World!'); // change console.log() -> print() before submitting on codeforces.↵
console.log(parameterVariable);↵
// Write a line of code that prints parameterVariable to stdout using console.log: ↵
}↵
↵
```↵
↵
### Testing Locally↵
↵
paste your input in a text file input.txt↵
↵
then↵
```↵
$ cat input.txt | node main.js↵
``` ↵
to view on console.↵
```↵
$ cat input.txt | node main.js > output.txt↵
```↵
to store the output in file.↵
↵
##### you can also make a sublime-build file for this and then just press Ctrl+B ↵
Tools> Build System > new build system > nodejs.sublime-build↵
```↵
{↵
"shell_cmd": "cat $file_path/input.txt | node $file > $file_path/output.txt"↵
}↵
```↵
↵
####Here is a [Sample Image](/predownloaded/a0/a8/a0a84d60c8171d55b1e50d133e6a3381681acb52.png) proof. :)↵
==================↵
↵
###To use in Codeforces↵
↵
`readline()` Reads one line from stdin.↵
↵
to get some space separated values from a line:↵
↵
```↵
var num = readline().split(" ").map(x => parseInt(x)); // num will be an array [1,2,3]↵
var x = num[0]; // to store values in different variables. ↵
var y= num[1];↵
var z= num[2];↵
↵
↵
var num2= readline(); // reads next line.↵
```↵
####For Output↵
```↵
print(x);↵
```↵
↵
####Refer [this submission](https://codeforces.com/contest/1199/submission/58184695) for a sample program submission.↵
↵
↵
↵
↵
###To run your programs locally.↵
`readline()` do not work by default if you try to use it locally on your computer.↵
↵
#### Write this header code in the beginning of your js code↵
↵
```↵
'use strict';↵
↵
process.stdin.resume();↵
process.stdin.setEncoding('utf-8');↵
↵
let inputString = '';↵
let currentLine = 0;↵
↵
process.stdin.on('data', inputStdin => {↵
inputString += inputStdin;↵
});↵
↵
process.stdin.on('end', _ => {↵
inputString = inputString.trim().split('\n').map(string => {↵
return string.trim();↵
});↵
↵
main(); ↵
});↵
↵
function readline() {↵
return inputString[currentLine++];↵
}↵
↵
// thats all what you have to write to get input from stdin, using readLine.↵
↵
↵
// Main code runs in main();↵
↵
↵
function main() {↵
const parameterVariable = readline();↵
↵
greeting(parameterVariable);↵
greeting(line2);↵
}↵
↵
function greeting(parameterVariable) {↵
console.log('Hello, World!'); // change console.log() -> print() before submitting on codeforces.↵
console.log(parameterVariable);↵
// Write a line of code that prints parameterVariable to stdout using console.log: ↵
}↵
↵
```↵
↵
### Testing Locally↵
↵
paste your input in a text file input.txt↵
↵
then↵
```↵
$ cat input.txt | node main.js↵
``` ↵
to view on console.↵
```↵
$ cat input.txt | node main.js > output.txt↵
```↵
to store the output in file.↵
↵
##### you can also make a sublime-build file for this and then just press Ctrl+B ↵
Tools> Build System > new build system > nodejs.sublime-build↵
```↵
{↵
"shell_cmd": "cat $file_path/input.txt | node $file > $file_path/output.txt"↵
}↵
```↵
↵
####Here is a [Sample Image](/predownloaded/a0/a8/a0a84d60c8171d55b1e50d133e6a3381681acb52.png) proof. :)↵