1

I'm trying to make a script that on input e.g "5" runs a readline question 5 times in a row. So if i put amount as 3, it should ask me 3 questions and will log answer 3 different times.

I've tried this, but can't get it to work.

var readline = require('readline');
var log = console.log;

var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});


rl.question("amounts to run", function(amounts){
var i;
for (i = 0; i < amounts; i++){
    rl.question("how is your day? ", function(answers){
        log(answers)
    })
}
});

rl.close();

Thank you in advance!

wulds
  • 61
  • 4
  • You can do this with promises and `async`/`await`. Have a look at [this question](https://stackoverflow.com/q/43638105/1048572) – Bergi Jun 30 '20 at 16:22
  • Does this answer your question? [How to get synchronous readline, or "simulate" it using async, in nodejs?](https://stackoverflow.com/questions/43638105/how-to-get-synchronous-readline-or-simulate-it-using-async-in-nodejs) – mlibby Jun 30 '20 at 19:22

1 Answers1

2

Bergi, thank you! I solved it.

const reader = require("readline-sync"); //npm install readline-syn
var log = console.log


let amount = reader.question("Amounts: ");
var i;
  for (i=0; i < amount; i++){
let answer = reader.question("How is your day? ")
log(answer)

};
wulds
  • 61
  • 4