As part of "The Odin Project" web-dev curriculum, I created a simple "Rock, Paper, Scissors" game. I am hoping to receive feedback on how readable the code is and if the organization is easy to follow.
I'm sure this code is convoluted and there are shorter and easier ways to do this. I have only been writing code for a month, so I am more interested in learning to write easy to read code than on optimization.
Are the variables named well? Is the organization of the code easy to follow? I tried to keep to small functions which each doing one small task. I don't know if it is wise to declare all global variables together at the top, or if they should be spread out between functions. I called all functions in one place at the bottom rather than having the calls spread out.
let humanScore = 0;
let computerScore = 0;
const rockButton = document.querySelector('#rockButton');
const paperButton = document.querySelector('#paperButton');
const scissorsButton = document.querySelector('#scissorsButton');
const scoreTextHuman = document.createElement('p');
const scoreTextComputer = document.createElement('p');
const message = document.createElement('p');
const computerMessage = document.createElement('p');
const userMessage = document.createElement('p');
let container = document.querySelector('#container');
let scoreCard = document.querySelector('#scoreCard');
let userInput;
let currentMessage;
let computerDecision;
let getComputerChoice = () => {
computerDecision = Math.random();
if (computerDecision < 0.33) {
computerDecision = "rock";
} else if (computerDecision > 0.33 && computerDecision < 0.66) {
computerDecision = "paper";
} else {
computerDecision = "scissors";
}
}
let displayScore = () => {
scoreCard.appendChild(scoreTextHuman);
scoreTextHuman.textContent = ("Your score: " + humanScore);
scoreCard.appendChild(scoreTextComputer);
scoreTextComputer.textContent = ("Computer score: " + computerScore);
}
let removeScoreAndMessages = () => {
scoreCard.removeChild(scoreTextComputer);
scoreCard.removeChild(scoreTextHuman);
scoreCard.removeChild(message);
scoreCard.removeChild(computerMessage);
scoreCard.removeChild(userMessage);
}
let displayMessage = () => {
if (currentMessage === "tie") {
scoreCard.appendChild(message);
message.textContent = "It is a tie."
scoreCard.appendChild(userMessage);
userMessage.textContent = "You chose: " + userInput;
scoreCard.appendChild(computerMessage);
computerMessage.textContent = "The computer chose: " +computerDecision;
} else if (currentMessage === "win") {
scoreCard.appendChild(message);
message.textContent = "Congratulations! You won that round."
scoreCard.appendChild(userMessage);
userMessage.textContent = "You chose: " + userInput;
scoreCard.appendChild(computerMessage);
computerMessage.textContent = "The computer chose: " +computerDecision;
} else {
scoreCard.appendChild(message);
message.textContent = "Oh no! It seems you lost that one."
scoreCard.appendChild(userMessage);
userMessage.textContent = "You chose: " + userInput;
scoreCard.appendChild(computerMessage);
computerMessage.textContent = "The computer chose: " +computerDecision;
}
}
let getUserChoice = () => {
let target = event.target;
switch(target.id) {
case 'rockButton':
userInput = 'rock';
switch(computerDecision) {
case 'rock':
currentMessage = "tie";
break;
case 'paper':
currentMessage = "loss";
computerScore++;
break;
case 'scissors':
currentMessage = "win";
humanScore++;
break;
}
break;
case 'paperButton':
userInput = 'paper';
switch(computerDecision) {
case 'rock':
currentMessage = "win";
humanScore++;
break;
case 'paper':
currentMessage = "tie";
break;
case 'scissors':
currentMessage = "loss";
computerScore++;
break;
}
break;
case 'scissorsButton':
userInput = 'scissors';
switch(computerDecision) {
case 'rock':
currentMessage = "loss";
computerScore++;
break;
case 'paper':
currentMessage = "win";
humanScore++;
break;
case 'scissors':
currentMessage = "tie";
break;
}
break;
}
};
let playRound = (event) => {
if (humanScore < 5 && computerScore < 5) {
getComputerChoice();
getUserChoice();
displayMessage();
displayScore();
container.addEventListener('mousedown', removeScoreAndMessages);
}
if (humanScore > 4) {
alert("Congratulations! You won!");
location.reload()
}
if (computerScore > 4) {
alert("The computer has won.");
location.reload()
}
}
container.addEventListener('mouseup', playRound);