I've tried changing the .textcontent to .innertext and then i get the error message "Cannot set properties of null (setting 'innertext')". Could it be that it's the HTML that is faulty or what because they are all showing the same error when run on chrome. What can I do please? ta
let cards = [];
let firstcard = randomNumber();
let secondcard = randomNumber();
let sum = firstcard + secondcard;
let blackjackvalue = false; // checks for blackjack value
let isalive = true; //to check if the user can play only when the game is true
let message = "";
let gamestate = document.getElementById(message); // get the gamestate
let sumEl = document.getElementById(sum); //holding the sum
let cardEl = document.getElementById("card"); //holding the card
let playerEl = document.getElementById("player"); //holding the player details
let playerdetails = {
name: "John",
wins: 5,
amount: "$" + 500,
};
playerEl.innertext = playerdetails.name + playerdetails.wins + playerdetails.amount;
function randomNumber() {
let jacknumbers = Math.floor(Math.random() * 14) + 1; //produces random numbers between 1 and 13 to be used by first and second card
return jacknumbers;
if (jacknumbers == 1) {
return 11;
} //these if conditions are for setting the ace card to either 1 or 11...not really sure
else if (jacknumbers > 10) {
return 10;
} else {
return jacknumbers;
}
}
function start() {
// let firstcard = randomNumber();
// let secondcard = randomNumber();
let cards = [firstcard, secondcard];
let sum = [firstcard + secondcard];
rendergamestate();
} //when this is called, it triggers the rendergamestate function
function rendergamestate() {
card.innertext = "cards:" + " " + firstcard + " " + secondcard;
for (i = 0; i < card.length; i += 1) {
card.innertext += cards[i] + " ";
//using the for loop to display the card contents
}
sumEl.innertext = "sum:" + " " + sum;
if (sum <= 20) {
message = "Do you want to draw a new card?";
blackjackvalue = false;
} else if (sum === 21) {
message = "Blackjack!";
blackjackvalue = true;
} else {
message = "You are out";
isalive = false;
blackjackvalue = false;
}
gamestate.innertext = message;
}
function newcard() {
//this button is for the user to add a new card to the already existing cards
if (isalive === true && blackjackvalue === false) {
let thirdcard = randomNumber();
sum += thirdcard; // sum=sum+thirdcard;
cards.push(thirdcard);
rendergamestate(); //calling the function to start the game
}
}