move endGame logic into new function

This commit is contained in:
lolabird 2023-11-07 21:24:44 -07:00
commit 0a63032994

View file

@ -4,6 +4,7 @@ let cScore = 0;
let ps; // Define the external variable for user's choice
const buttons = document.querySelectorAll('button');
buttons.forEach((button) => {
button.addEventListener('click', () => {
ps = button.id; // Assign the button's id to the external variable
@ -11,6 +12,8 @@ buttons.forEach((button) => {
});
});
function getComputerChoice(){
let compAnswer = Math.floor(Math.random() * 3) + 1;
@ -76,15 +79,21 @@ function game() {
}
}
if (p !== c) {
let winner = (p > c) ? true : false;
if (pScore === 5 || cScore === 5) {
console.log(endGame());
}
}
function endGame(){
if (pScore !== cScore) {
let winner = (pScore > cScore) ? true : false;
if (winner) {
return "You won the game " + p + " to " + c + "! Great job!" ;
return "You won the game " + pScore + " to " + cScore + "! Great job!" ;
} else {
return "You lost the game " + p + " to " + c + ". Better luck next time!";
return "You lost the game " + pScore + " to " + cScore + ". Better luck next time!";
}
} else if (c === 0) {
} else if (cScore === 0) {
return "Uh... I don't think you played the game right. Refresh the page and try that again."
} else {
return "Whoa, you tied! Refresh for a rematch!";