I wrote this bit of code
while True:
text = input('type here > ')
print text
I've tried the below but it doesnt seem to be working
I'm having bit of a struggle trying to create a JavaScript version of it, since it seems js while loops constantly rerun but python seems to wait for input before rerunning. I am using the readline module to receive input from the console.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
while (true) {
rl.question('type here > ', text => {
console.log(text)
})
}
Is there something im getting wrong? Im fairly new to programming
Any solutions?
If what you are trying to do is get input from the console in JavaScript you can use NodeJS process.stdin the implementation below :
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let input = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
input += inputStdin;
});
process.stdin.on('end', _ => {
input = input.trim()
.split('\n')
.map(str => str.trim());
// calling the function that works on the input
doSomething();
});
function readInput() {
return input[currentLine++];
}
Sample function to do something with what you get from the console
function doSomething() {
console.log(readInput())
}