This is not a duplicate because window.load() refreshes after 3 seconds without letting the user see the result fully
What I want to do is have the game reset automatically when the user clicks another button to make a new choice without having to refresh the page to click a "reset" button.
Would this involve clearing the innerHTMLs and resetting the variables onclick before running the script again?
My code is here: http://jsbin.com/yuvalucupo/edit?html,css,js
//var rock = document.getElementById("rock");
//var paper = document.getElementById("paper");
//var scissors = document.getElementById("scissors");
var screen = document.getElementById("screen");
var compAnswer = Math.random();
//Set computer value
if (compAnswer >= 0 && compAnswer <= 0.33) {
compAnswer = "rock";
} else if (compAnswer >= 0.34 && compAnswer <= 0.66) {
compAnswer = "paper";
} else {
compAnswer = "scissors";
}
function result(choice1, choice2) {
if (choice1 === choice2) {
screen.innerHTML = "<p>It's a tie!</p>";
}
//Rock Start
else if (choice1 === "rock" && choice2 === "scissors") {
screen.innerHTML = "<p>Rock Wins!</p>";
} else if (choice1 === "rock" && choice2 === "paper") {
screen.innerHTML = "<p>Paper Wins!</p>";
}
//End Rock
//Paper Start
else if (choice1 === "paper" && choice2 === "scissors") {
screen.innerHTML = "<p>Scissors Wins!</p>";
} else if (choice1 === "paper" && choice2 === "rock") {
screen.innerHTML = "<p>Paper Wins</p>!";
}
//Paper End
//Scissors Start
else if (choice1 === "scissors" && choice2 === "paper") {
screen.innerHTML = "<p>Scissors Wins</p>";
} else if (choice1 === "scissors" && choice2 === "rock") {
screen.innerHTML = "<p>Rock Wins</p>";
}
//Scissors End
}
//Display Computer's Answer
function displayCompAnswer() {
var computer = document.getElementById("computerAnswer");
computer.innerHTML = compAnswer;
}
#screen, #computerAnswer {
height: 100px;
width: 250px;
background-color: lightgrey;
border: 1px solid black;
}
<div id="screen"></div>
<button id="rock" onclick="userAnswer = 'rock'; result(userAnswer, compAnswer); displayCompAnswer();">Rock</button>
<button id="paper" onclick="userAnswer = 'paper'; result(userAnswer, compAnswer); displayCompAnswer();">Paper</button>
<button id="scissors" onclick="userAnswer = 'scissors'; result(userAnswer, compAnswer); displayCompAnswer();">Scissors</button>
<div id="computerAnswer"></div>