When working with nodejs outputting data to the console is pretty straight-forward. However reading data from the console is a little bit tricky, at least not as straight-forward as it is in some other languages like c and c++. In this article, we will see how we can use nodejs to:
- Read data from the console
- format the data we read
- output the data with or without a new line (console.log always creates a new line)
To demonstrate how all these come together, we will write a code snippet that takes in two numbers from the terminal, finds their common divisors, and outputs the divisors on the same line, then outputs the sum of the common divisors on a new line.
Reading data from console
To read data from the console we will need the inbuilt nodejs "readline" module which provides an interface for reading data from a Readable stream (such as process.stdin) one line at a time. Using some methods in the readline module, such as createInterface, on, once, close, you will be able to read data from the console.
Formatting data
While inputting data we may want to do some data conversions or perform some operations on the data we read. you will find some of the following arrays, string, and number methods helpful - Number, toString, split, splice, slice, map, e.t.c.
Outputting data
To output data to the console, we would usually use console.log. However, this automatically creates a new line. If you want to output data without the newline, then you can use "process.stdout.write()"
Demo
1.Import and setup the readline module
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
process.stdin.setEncoding('utf8');
rl.once('line',line => {
process.exit()
})
2.Format the data
const firstLine = line.toString().split(' ')
const num1 = Number(firstLine[0])
const num2 = Number(firstLine[1])
3.Adding the actual logic to solve the problem
const sumOfDivisors = findDivisors(num1,num2)
console.log(sumOfDivisors)
// This is obviously not the most efficient way to solve this problem,
// but chose this so as not to convolute the example
const findDivisors = (num1, num2) =>{
const lesserNum = num1 < num2 ? num1 : num2;
let divisors = 0
for (let i = lesserNum; i > 0; i--) {
if((num1%i === 0) && (num2%i === 0) ){
process.stdout.write(i + " ")
divisors += i
}
}
process.stdout.write('\n');
return divisors
}
Bringing everything together
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
process.stdin.setEncoding('utf8');
rl.once('line',line => {
const firstLine = line.toString().split(' ')
const num1 = Number(firstLine[0])
const num2 = Number(firstLine[1])
const sumOfDivisors = findDivisors(num1,num2)
console.log(sumOfDivisors)
process.exit()
})
const findDivisors = (num1, num2) =>{
const lesserNum = num1 < num2 ? num1 : num2;
let divisors = 0
for (let i = lesserNum; i > 0; i--) {
if((num1%i === 0) && (num2%i === 0) ){
process.stdout.write(i + " ")
divisors += i
}
}
process.stdout.write('\n');
return divisors
}
Hopefully, this demo has been able to show you how to handle input in nodejs, If you would like to see other methods that can help you read data from nodejs input, check out the nodejs documentation here.