While making a BlackJack game, In line 25 in JS I am trying to tell JavaScript to redeclare the .textContent of the variable messageEl to the variable message whenever the user clicks on the button but it’s giving the error Uncaught Typeerror: Cannot set properties of null (setting ‘textContent’)
HTML:
<h1>Blackjack</h1>
<p>Want to play a round?</p>
<p>Cards: </p>
<p>Sum: </p>
<button onclick="startGame">START GAME</button>
Javascript:
let firstCard = 2
let secondCard = 1
let sum = firstCard + secondCard
let isAlive = true
let hasBlackJack = false
let message = ""
let messageEl = document.getElementById("messageEl")
function startGame() {
if (sum <= 20) {
message = "Do you want a draw?"
} else if (sum === 21) {
hasBlackJack = true
message = "Whoa! You've won a BlackJack"
} else {
isAlive = false
message = "You lost"
}
messageEl.textContent = message // this is the line that's making the problem and I have no clue what makes it wrong!
}