I am making a very basic game in Node.js. Because it's not working, I've made it even more basic - if a user enters 'red' they win, 'blue' they don't.
The do...while loop should get the user's input, evaluate it, and stop the loop when they enter 'red' i.e. win.
My problem is that it isn't getting the user input before repeating the loop - it just asks the user the question endlessly in an infinite loop without letting them input anything? How can I fix this? Code below:
I declared a variable 'won':
const won = false;
I have a function getInput() that prompts the user and returns the input
function getInput() {
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Red or blue?'), color => {
return color
readline.close();
}
}
Then the do...while loop which should evaluate the user input and set won to true or false, with true ending the loop:
do {
let color = getInput()
if (color === "red") {
console.log("You win!");
won = true
}
else {
console.log("Try again!")
}
}
while (won === false)