From bd24adc2d798a853ce0ed7d887834271955c3221 Mon Sep 17 00:00:00 2001 From: Synobbol <97944378+Synobbol@users.noreply.github.com> Date: Tue, 17 Jan 2023 18:50:15 +0100 Subject: [PATCH] create rounds and keep score --- script.js | 71 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/script.js b/script.js index 8eff902..da194d7 100644 --- a/script.js +++ b/script.js @@ -45,33 +45,74 @@ function playRound(playerSelection, computerSelection){ playerSelection = playerSelection.toLowerCase(); if (playerSelection === "rock" && computerSelection == "paper"){ - console.log(playerSelection, computerSelection); - return result = "You Lose! Paper beats Rock"; + result = "You Lose! Paper beats Rock"; + console.log(playerSelection, computerSelection, result); + + return false; } else if(playerSelection === "paper" && computerSelection == "rock"){ - console.log(playerSelection, computerSelection); - return result = "You Win! Paper beats Rock"; + result = "You Win! Paper beats Rock"; + console.log(playerSelection, computerSelection, result); + + return true; } else if(playerSelection === "paper" && computerSelection == "scissors"){ - console.log(playerSelection, computerSelection); - return result = "You Lose! Scissors beats Paper"; + result = "You Lose! Scissors beats Paper"; + console.log(playerSelection, computerSelection, result); + + return false; } else if(playerSelection === "scissors" && computerSelection == "paper"){ - console.log(playerSelection, computerSelection); - return result = "You Win! Scissors beats Paper"; + result = "You Win! Scissors beats Paper"; + console.log(playerSelection, computerSelection, result); + + return true; } else if(playerSelection === "rock" && computerSelection == "scissors"){ - console.log(playerSelection, computerSelection); - return result = "You Win! Rock beats Scissors"; + result = "You Win! Rock beats Scissors"; + console.log(playerSelection, computerSelection, result); + + return true; } else if(playerSelection === "scissors" && computerSelection == "rock"){ - console.log(playerSelection, computerSelection); - return result = "You Lose! Rock beats Scissors"; + result = "You Lose! Rock beats Scissors"; + console.log(playerSelection, computerSelection, result); + + return false; } else{ - console.log(playerSelection, computerSelection); - return result; + console.log(playerSelection, computerSelection,result); + return null; } } -console.log(playRound("Paper", getComputerChoice())); +// console.log(playRound("Paper", getComputerChoice())); + +function game(){ + let player = 0; + let computer = 0; + let win = "You win"; + + while (player || computer < 5) { + let party = playRound(prompt("Rock, Paper or Scissors?"), getComputerChoice()); + + if(party === true){ + player +=1; + } + else if(party === false){ + computer +=1; + } + + console.log(`Player score: ${player}/5, Computer score: ${computer}/5`); + + if(player === 5){ + return win = "You win!"; + } + else if(computer === 5){ + return win = "Computer wins!"; + } + } + +} + +console.log(game()); \ No newline at end of file