I can’t seem to see what I’m missing in my conditional statements. When the button’s are clicked on the page the textContent
isn’t updating.
const btn1 = document.querySelector('.btn1');
const btn2 = document.querySelector('.btn2');
const btn3 = document.querySelector('.btn3');
const playerChoice = [btn1, btn2, btn3];
const computerChoice = ['rock','paper','scissors']
// Game options rock[0] , paper[1] , scissors[2]
// Lapis, Papyrus, Scalpellus;
const startGame = () => {
playerChoice.forEach(options => {
options.addEventListener('click', function(){
const rantInt = Math.floor(Math.random() * 3);
const pcOptions = computerChoice[rantInt];
//Callback to compare choices
compareChoices(this.innerHTML, pcOptions);
});
});
};
startGame();
const compareChoices = (player, computer) => {
const result = document.querySelector('.winner');
if ( player === computer) {
result.textContent = 'TIE'
}
else if (player == 'Lapis' && computer == 'Scalpellus') {
result.textContent = 'Player won! Lapis beats Scalpellus.';
}
else if (computer == 'Lapis' && player == 'Scalpellus') {
result.textContent = 'Computer won! Lapis beats Scalpellus';
}
else {
return;
}
};
text-align: {
center;
}
h2 {
margin: 60px;
text-align: center;
}
.button {
text-align: center;
font-family: 'Rubik', sans-serif;
background-color: #E44949;
}
.btn1 {
margin: 25px;
width: 70px;
height: 30px;
text-align: center;
font-family: 'Rubik', sans-serif;
background-color: #FA9696;
}
.btn2 {
margin: 25px;
width: 70px;
height: 30px;
text-align: center;
font-family: 'Rubik', sans-serif;
background-color: #FA9696;
}
.btn3 {
margin: 25px;
width: 80px;
height: 30px;
text-align: center;
font-family: 'Rubik', sans-serif;
background-color: #FA9696;
color: black;
}
<style>
@import url('https://fonts.googleapis.com/css2?family=Londrina+Shadow&family=Rubik:ital,wght@1,300&display=swap');
</style>
<h1>Let's play! Click on the buttons.</h1>
<h1>
<button class="btn1">Lapis</button>
<button class="btn2">Papyrus</button>
<button class="btn3">Scalpellus</button>
</h1>
<h2 class="winner"></h2>
<script src="script.js"></script>
</body>
</html>