I want to get input values from the console. Although I am getting one input values, the console doesn't return more than one input value.
When I as soon as entering the '2' number, the code terminates.
Like below,
2
Dates: undefined
Dates: undefined
But, I want to enter the input value twice.
Like below,
2
10/11/2009
11/10/2010
Dates: Sunday
Dates: Wednesday
'use strict'
process.stdin.resume();
process.stdin.setEncoding('utf-8');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: console.log
})
// The days of the week are: "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
function getDayName(dateString) {
let dayName;
// Write your code here
var day_names = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var date = new Date(dateString)
dayName = day_names[date.getDay()];
return dayName;
}
rl.on('line', input => {
const d = input.trim().split('')
for (let i = 0; i < d; i++) {
const a = input.trim().split('\n').map(string => {
return string.trim();
});
console.log('Dates: ', getDayName(a));
}
rl.close();
});
I am not sure what the readline library does, this works for me:
const process = require('process');
process.stdin.setEncoding('utf8');
let results = [];
process.stdin.on('readable', () => {
let chunk;
while ((chunk = process.stdin.read()) !== null) {
results.push(getDayName(chunk));
// 2 = amount of lines you want to read
if (results.length === 2) {
for (let i = 0; i < results.length ;i++) {
console.log("Dates: " + results[i]);
}
//if you do not do a break this will go on forever until you press ctrl + c
break;
}
}
});
I hope I helped
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.