Help | Rock, Paper, Scissors-no idea how to reset game to the beginning-thank you!


#1

//origin point for keeping track of scores
var computerScore = 0;
var userScore = 0;
var clickCount = 0;

	//grab the buttons
	var button = document.querySelectorAll("button");

	//assign event listener to each button and return userInput
	button.forEach(function(button) {
		button.addEventListener("click", function(e) {
			input = button.id;
			console.log(input);
			return input;
		});

		//get user input and initialize/activate game function with every click
		button.addEventListener("click", game);
		button.onclick = function() {
			console.log(clickCount);
			return clickCount++;
		}
	});

	function game() {
		
		function computerOutput() {//provides computer output
			var itemsArray = ["rock", "paper", "scissors"];
			var randomOutput = itemsArray[Math.floor(Math.random() * itemsArray.length)];
			var computerChoiceText1 = document.getElementById("computerChoiceText1");
			computerChoiceText1.textContent = "The computer chooses" + " " + randomOutput;
			return randomOutput;
		}
		var output = computerOutput();
		
		function compareInputAndOutput(userInput, computerOutput) {//compares input w/output

			var computerChoiceText = document.getElementsByClassName("computerChoiceText2");
			for (var i = 0; i < computerChoiceText.length; i++) {
				if (userInput === computerOutput) {
					//computerScore = computerScore + null;
					//userScore = userScore + null;
					computerChoiceText[i].textContent = "It's a tie, try again…";

				} else if (userInput === "rock" && computerOutput === "paper") {
					computerScore++;
					computerChoiceText[i].textContent = "Paper covers rock! System wins this one.";
				} else if (userInput === "rock" && computerOutput === "scissors") {
					userScore++;
					computerChoiceText[i].textContent = "Rock breaks scissors! User wins this one.";
				} else if (userInput === "paper" && computerOutput === "rock") {
					userScore++;
					computerChoiceText[i].textContent = "Paper covers rock! User wins this one.";
				} else if (userInput === "paper" && computerOutput === "scissors") {
					computerScore++;
					computerChoiceText[i].textContent = "Scissors cuts paper! System wins this one.";
				} else if (userInput === "scissors" && computerOutput === "rock") {
					computerScore++;
					computerChoiceText[i].textContent = "Rock breaks scissors! System wins this one.";
				} else if (userInput === "scissors" && computerOutput === "paper") {
					userScore++;
					computerChoiceText[i].textContent = "Scissors cuts paper! User wins this one.";
				}
			}

		}
		console.log(compareInputAndOutput(input, output));


		function keepScore(computer, user) {//keeps score

			var scoreText = document.getElementsByClassName("scoreText");
			for (var i = 0; i < scoreText.length; i++) {

				if (clickCount === 4 && computerScore > userScore) {
					scoreText[i].textContent = "Game, computer wins with" + " " + computerScore + " " + "points, user had" + " " + userScore + " " + "points";
				} else if (clickCount === 4 && userScore > computerScore) {
					scoreText[i].textContent = "Game, you win with" + " " + userScore + " " + "points, computer had" + " " + computerScore + " " + "points";
				} else if (clickCount === 4 && computerScore === userScore) {
					scoreText[i].textContent = "It's a draw! computer has" + " " + computerScore + " " + "points, user has" + " " + userScore + " " + "points";
				}
			}

			function resetScore(clicks) {//resets score
				if (clickCount === 4) {
					computerScore = 0;
					userScore = 0;
					clickCount = 0;
				}
			}
			resetScore(clickCount);
		}
		console.log(keepScore(computerScore, userScore));
	}

#2

You could throw in document.location.reload(false)

the false argument means it won’t go and grab a copy from the server again and just use the locally cached copy. You could use true if you did want it to go and grab it from the server again.


#3

Thank you so much-i attached it to a ‘click’ event which fired a function containing the code you suggested…it works! Thanks again!