Hello, I’m trying to write a rock, paper, scissors game and the checkWinner function doesn’t seem to do anything when the score gets to 5, the scores continue incrementing past 5, but no message appears. I wonder if anyone could tell me what I’ve done wrong and if I’ve called it correctly, please
Many thanks
let choice
let computerChoice
let userScore = parseInt(document.getElementById("user-score"));
let computerScore = parseInt(document.getElementById("computer-score"));
document.addEventListener("DOMContentLoaded", function() {
let buttons = document.getElementsByTagName("button");
for (let button of buttons) {
button.addEventListener("click", function() {
choice = this.getAttribute("data-type");
runGame();
})
}
runGame();
})
function runGame() {
let computerChoices = ["rock", "paper", "scissors"];
let randomChoice = Math.floor(Math.random() * 3);
computerChoice = computerChoices[randomChoice];
let result = document.getElementById("result")
if (choice == computerChoice) {
result.innerText = "It's a draw!";
}
else if (choice == "rock") {
if (computerChoice == "scissors"){
incrementPlayerScore();
result.innerText = "You won!";
} else {
incrementComputerScore();
result.innerText = "You lost!";
}
}
else if (choice == "paper") {
if (computerChoice == "rock"){
incrementPlayerScore();
result.innerText = "You won!";
} else {
incrementComputerScore();
result.innerText = "You lost!";
}
}
else if (choice == "scissors") {
if (computerChoice == "paper"){
incrementPlayerScore();
result.innerText = "You won!";
} else {
incrementComputerScore();
result.innerText = "You lost!";
}
}
checkWinner();
}
function checkWinner() {
let winner = document.getElementById("winner");
if (userScore == 5) {
winner.innerText = "Congratulations! You won 5 times!";
} else if (computerScore == 5) {
winner.innerText = "Oh no! The computer won 5 times!";
}
}
function incrementPlayerScore() {
let oldScore = parseInt(document.getElementById("user-score").innerText);
document.getElementById("user-score").innerText = ++oldScore;
}
function incrementComputerScore() {
let oldScore = parseInt(document.getElementById("computer-score").innerText);
document.getElementById("computer-score").innerText = ++oldScore;
}