I have created a pretty simple rock-paper-scissors game that uses event listeners and manipulates the DOM. I would appreciate any constructive feedback. Whether it is readability, variable names, security, literally ANYTHING would be greatly appreciated.
My code can be found here: https://github.com/alvinalic1/RockPaperScissors.git
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class = title>
<h1>ROCK ----- PAPER ----- SCISSORS</h1>
</div>
<div class="score">
<h2>SCORE</h3>
<h3 id="your-score">You: 0 </h3>
<h3 id="comp-score">Computer: 0 </h3>
</div>
<div class="player">
<div class = "you">
<h2 class="your-choice">YOUR CHOICE</h2>
<img src="images/QuestionMark.jpg">
<div class = "buttons">
<button id = "rock">ROCK</button>
<button id = "paper">PAPER</button>
<button id = "scissors">SCISSORS</button>
</div>
</div>
<div class = "computer">
<h2>COMPUTER CHOICE</h2>
<img src="images/QuestionMark.jpg" id="comp">
</div>
</div>
<script src="javascript.js"></script>
</body>
</html>
//array holding possible choices
const RPS = ["Rock", "Paper", "Scissors"];
let playerChoice = "";
let computerScore = 0;
let playerScore = 0;
/*
Method that will get a random number and use that number to access one of the choices in the RPS Array
*/
const computer = document.querySelector(".computer");
const compImg = document.querySelector("#comp");
function getComputerChoice()
{
let number = Math.floor(Math.random()*RPS.length)
if(RPS[number] == "Rock"){
compImg.src="images/rock.jpg";
}
if(RPS[number] == "Paper"){
compImg.src="images/paper.jpg";
}
if(RPS[number] == "Scissors"){
compImg.src="images/Scissors.jpg";
}
return RPS[number];
}
/*
Compares the computer choice with the player choice
returns winner
*/
function playRound(getComputerChoice, getPlayerChoice)
{
let player = getPlayerChoice.toLowerCase();
let computer = getComputerChoice.toLowerCase();
if(computerScore == 5){
alert("GAME OVER COMPUTER WINS");
}else if(playerScore == 5){
alert("GAME OVER YOU WIN");
}
if(player === computer)
{
return "TIE GAME";
}
if((computer == "scissors" && player == "rock") ||
(computer == "rock" && player == "paper") ||
( computer == "paper" && player == "scissors")){
computerScore++;
// const node = document.createTextNode(`${computerScore} times`)
// compScore.appendChild(node);
compScore = document.getElementById("comp-score");
compScore.innerHTML = ("Computer: " + computerScore);
return `You LOSE :( ${player} beats ${computer}`;
}else{
playerScore++;
urScore = document.getElementById("your-score");
urScore.innerHTML = ("You: "+playerScore)
return `You WIN! ${computer} beats ${player} `;
}
}
const questionMark = document.querySelector("img");
const rock = document.querySelector("#rock");
const paper = document.querySelector("#paper");
const scissors = document.querySelector("#scissors");
const buttons = document.querySelector(".buttons");
const you = document.querySelector(".you");
const img = document.querySelector("img");
var urScore = document.querySelector("#your-score");
var compScore = document.querySelector("#comp-score");
/*
The next three methods are all meant to update the image so it coencides
with the player choice
*/
function showRock(){
img.src="images/rock.jpg";
you.insertBefore(img, buttons);
}
function showPaper(){
img.src="images/paper.jpg";
you.insertBefore(img, buttons);
}
function showScissors(){
img.src="images/scissors.jpg";
you.insertBefore(img, buttons);
}
/*
The next 3 methods are all intended to be even listeners
Once the player picks their choice
The image updates and the score is displayed
*/
rock.addEventListener("click", () =>{
playerChoice = "rock";
showRock();
console.log(playRound(playerChoice, getComputerChoice()));
})
paper.addEventListener("click", () =>{
showPaper();
playerChoice = "paper";
console.log(playRound(playerChoice, getComputerChoice()));
});
scissors.addEventListener("click", () =>{
playerChoice = "scissors";
showScissors();
console.log(playRound(playerChoice, getComputerChoice()));
});