I am trying to complete my first coding challenge as i am very new to coding, and i have to admit i am struggling with showing a scorecard that keeps track of the player vs computer score over x amount of games played.
I have attempted all kinds of ways to do this including loops, and multiple functions, I feel I have been on this for far too long and obviously need help as my efforts are in vein as i can not increase the scores each time a player or computer wins.
I have gone back to basics and showing you the code below, at the moment the score only goes to a maximum number of 1 for both player and computer and does not continue increasing after that for each win made.
Can you help?
//Global Variabes.
var playerCard = 0;
var computerCard = 0;
for (var i = 0; i < document.querySelectorAll("button").length; i++) {
document.querySelectorAll("button")[i].addEventListener("click", function() {
var playerChoice = (this.innerHTML);
var random = Math.floor(Math.random() *
document.querySelectorAll("button").length);
var choices = ["Rock", "Paper", "Scissors"];
var compChoice = choices[random];
document.querySelector(".scoreCard").style.display = "block";
document.querySelector(".player").innerHTML = playerChoice;// view players choice.
document.querySelector(".computer").innerHTML = compChoice;// view computers choice.
if (playerChoice === "Rock" && compChoice === "Scissors") {
document.querySelector(".result").innerHTML = "Player Wins.";
} else if (playerChoice === "Scissors" && compChoice === "Paper") {
document.querySelector(".result").innerHTML = "Player Wins.";
} else if (playerChoice === "Paper" && compChoice === "Rock") {
document.querySelector(".result").innerHTML = "Player Wins.";
} else if (playerChoice === "Scissors" && compChoice === "Rock") {
document.querySelector(".result").innerHTML = "Computer Wins.";;
} else if (playerChoice === "Paper" && compChoice === "Scissors") {
document.querySelector(".result").innerHTML = "Computer Wins.";;
} else if (playerChoice === "Rock" && compChoice === "Paper") {
document.querySelector(".result").innerHTML = "Computer Wins.";;
} else if (playerChoice === compChoice) {
document.querySelector(".result").innerHTML = "It's a draw";
}
function trackPlayerScore() {
if (document.querySelector(".result").firstChild.wholeText === "Player Wins.") {
document.querySelector(".pRecord").innerHTML = playerCard+1;
} else if (document.querySelector(".result").firstChild.wholeText === "Computer Wins.") {
document.querySelector(".cRecord").innerHTML = computerCard+1;}
}//end of trackPlayerScore function.
trackPlayerScore()
})// end of anonymous function.
}// end of initial loop..