User Input

Learn how to get the user’s input in the console.

What have we got?

We have set up taking the number of rounds from the user and the game’s introduction. This is our inventory of already created functions:

// Function to display game instructions
function displayInstructions(){
console.log('Welcome to Rock Paper Scissors.\n');
console.log('The rules are simple:');
console.log('1. Rock beats scissors');
console.log('2. Scissors beat paper');
console.log('3. Paper beats rock');
}
// Function to ask for the number of rounds
function getRounds(){
let rounds;
do {
rounds = prompt('How many rounds would you like to play? \nRounds: ');
rounds = parseInt(rounds);
if (isNaN(rounds) || rounds <= 0) {
console.log('Please enter a valid positive integer for rounds.');
}
} while (isNaN(rounds) || rounds <= 0);
return rounds;
}
// Function to contain the flow of the whole program
function main(){
displayInstructions();
const rounds = getRounds();
}
Inventory of created functions

In this task, we ...