Joining the Dots

Learn how to set up the main function using all functions.

What have we got?

We have catered to all the components required to play a round. All the essential functionalities are there, how will we implement the code for multiple rounds? 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 get the player's choice
function getPlayerChoice(){
const validChoices = ['rock', 'paper', 'scissors'];
let playerChoice;
do {
playerChoice = prompt('Please enter rock, paper, or scissors. \nChoice: ').toLowerCase();
if (!validChoices.includes(playerChoice)) {
console.log('Invalid choice. Please enter either rock, paper, or scissors.');
}
} while (!validChoices.includes(playerChoice));
return playerChoice;
}
// Function to get the computer’s choice
function getComputerChoice(){
const options = ['rock', 'paper', 'scissors'];
const computerChoice = options[Math.floor(Math.random() * options.length)];
console.log('Computer chooses', computerChoice);
return computerChoice;
}
// Function to determine the winner of the round
function determineRoundWinner(playerChoice, computerChoice) {
if (playerChoice === computerChoice) {
console.log('Tie!');
return 'tie';
} else if (playerChoice === 'rock' && computerChoice === 'scissors') {
console.log('You win!');
return 'player';
} else if (playerChoice === 'rock' && computerChoice === 'paper') {
console.log('Computer wins!');
return 'computer';
} else if (playerChoice === 'paper' && computerChoice === 'rock') {
console.log('You win!');
return 'player';
} else if (playerChoice === 'paper' && computerChoice === 'scissors') {
console.log('Computer wins!');
return 'computer';
} else if (playerChoice === 'scissors' && computerChoice === 'rock') {
console.log('Computer wins!');
return 'computer';
} else if (playerChoice === 'scissors' && computerChoice === 'paper') {
console.log('You win!');
return 'player';
}
}
// Function to contain the flow of the whole program
function main(){
let playerScore = 0
let computerScore = 0
displayInstructions();
const rounds = getRounds();
const playerChoice = getPlayerChoice();
const computerChoice = getComputerChoice();
const winner = determineRoundWinner(playerChoice, computerChoice);
if(winner === 'player'){
playerScore++;
} else if(winner === 'computer'){
computerScore++;
}
console.log('Player score:', playerScore, '| Computer score:', computerScore);
}
Inventory of created functions
...