I followed a tutorial that goes through different practical examples and challenges using vanilla Javascript, HTML and CSS. I just completed a simple rock, paper, scissors game; however the tutorial does not really focus on code quality but rather on understanding first. Before I move on to the next challenge I want to identify any issues or bad practices so I can begin developing good coding habits.
// Challenge 3: Rock, Paper, Scissors
function rpsGame(yourChoice) {
const choices = ['rock', 'paper', 'scissors'];
let botChoice = choices[randToRpsIndex()];
let results = isWinner(yourChoice, botChoice);
modifyFrontEnd(yourChoice, botChoice, results);
function randToRpsIndex() {
return Math.floor(Math.random() * 3);
}
function isWinner(yourChoice, botChoice) {
let winners = {
'rock': 'scissors',
'paper': 'rock',
'scissors': 'paper'
}
if (botChoice === yourChoice) {
return [true, true];
}
if (botChoice === winners[yourChoice]) {
return [true, false];
} else {
return [false, true]
}
}
function modifyFrontEnd(yourChoice, computerChoice, results) {
let yourChoiceObj = document.getElementById(yourChoice),
botChoiceObj = document.getElementById(computerChoice);
let flexBoxDiv = document.getElementById('flex-box-rps-div');
// Clear the div
flexBoxDiv.innerHTML = "";
// If both choices are the same clone the image
if (yourChoiceObj == botChoiceObj) {
botChoiceObj = yourChoiceObj.cloneNode(true);
}
yourChoiceObj.id = 'your-choice', botChoiceObj.id = 'bot-choice';
yourChoiceDiv = document.createElement('div'), botChoiceDiv = document.createElement('div'), messageDiv = document.createElement('div');
let [yourScore, botScore] = results;
messageText = document.createElement('h2');
scores = {
yourScore,
botScore
};
choiceDivs = {
yourChoiceDiv,
botChoiceDiv
};
modifyStyle(scores, choiceDivs, messageText);
yourChoiceDiv.append(yourChoiceObj);
botChoiceDiv.append(botChoiceObj);
messageDiv.append(messageText);
flexBoxDiv.append(yourChoiceDiv, messageDiv, botChoiceDiv);
}
function modifyStyle(scores, divs, messageText) {
messageText.style.fontSize = "20px";
let {
yourScore,
botScore
} = scores, {
yourChoiceDiv,
botChoiceDiv
} = divs;
// If player wins
if (yourScore == true && botScore == false) {
yourChoiceDiv.style.boxShadow = "10px 10px 10px green";
botChoiceDiv.style.boxShadow = "10px 10px 10px red";
messageText.style.color = "green";
messageText.textContent = "You Won!";
}
// If player loses
else if (yourScore == false && botScore == true) {
yourChoiceDiv.style.boxShadow = "10px 10px 10px red";
botChoiceDiv.style.boxShadow = "10px 10px 10px green";
messageText.style.color = "red";
messageText.textContent = "You Lost!";
}
// If player draws
else if (yourScore == true && botScore == true) {
yourChoiceDiv.style.boxShadow = "10px 10px 10px blue";
botChoiceDiv.style.boxShadow = "10px 10px 10px blue";
messageText.style.color = "blue";
messageText.textContent = "You Tied!"
}
}
}
.flex-box-container-1,
.flex-box-rps {
display: flex;
border: 1px solid black;
padding: 10px;
flex-wrap: wrap;
flex-direction: row;
justify-content: space-around;
}
.flex-box-container-1 div {
padding: 10px;
border: 1px solid black;
display: flex;
align-items: center;
}
.container-1 {
border: 1px solid black;
align-items: center;
}
.container-2 {
border: 1px solid red;
width: 75%;
margin: 0 auto;
/*Centralized*/
text-align: center;
}
.flex-box-container-2 {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
border: solid blue;
}
.flex-box-container-2 img {
border: 1px solid green;
padding: 1px;
margin: 5px;
}
.flex-box-rps img:hover {
box-shadow: 0px 10px 50px rgba(37, 50, 223, 1);
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="static/style.css">
<title>Javascript on Steroids</title>
</head>
<body>
<div class="container-3">
<h2>Challenge 3: Rock, Paper, Scissors</h2>
</div>
<div class="flex-box-rps" id="flex-box-rps-div">
<img id="rock" src="https://www.pngkit.com/png/detail/173-1734891_cartoon-rock-clip-art-clipart-clip-art.png" height=150 width=150 alt="" onclick="rpsGame(this.id)">
<img id="paper" src="https://www.seekpng.com/png/detail/412-4124702_note-clipart-piece-paper-cartoon-document.png" height=150 width=150 alt="" onclick="rpsGame(this.id)">
<img id="scissors" src="https://cdn5.vectorstock.com/i/1000x1000/36/54/scissors-cartoon-vector-21843654.jpg" height=150 width=150 alt="" onclick="rpsGame(this.id)">
</div>
<script src="static/script.js">
</script>
</body>
</html>