I'm trying to build a game of Rock, Scissor, Paper. The code isn't working for some reason. Could anyone shed some light on please. Could mostly be some syntax errors. (I'm a beginner)
var pcChoices = ["r", "s", "p"];
var wins = 0;
var losses = 0;
var ties = 0;
document.onkeyup = function(event) {
var userGuess = event.key;
var pcGuess = pcChoices[Math.floor(Math.random() * pcChoices.length)];
if ((userGuess === "r") || (userGuess === "s") || (userGuess === "p")) {
if ((userGuess === "r") && (pcGuess === "s")) {
wins++;
} else if ((userGuess === "r") && (pcGuess === "p")) {
losses++;
} else if ((userGuess === "s") && (pcGuess === "r")) {
losses++;
} else if ((userGuess === "s") && (pcGuess === "p")) {
wins++;
} else if ((userGuess === "p") && (pcGuess === "r")) {
wins++;
} else if ((userGuess === "p") && (pcGuess === "s")) {
losses++;
} else if (userGuess === pcGuess) {
ties++;
}
var html =
"<p>You chose: " + userGuess + "</p>" +
"<p>The pc chose: " + pcGuess + "</p>" +
"<p>wins: " + wins + "</p>" +
"<p>ties: " + ties + "</p>";
document.querySelector("#game").innerHTML = html;
}
};
<div id="game"></div>
Aucun commentaire:
Enregistrer un commentaire