I have created a simple rock paper scissors game, with my limited HTML/JavaScript knowledge. I've created a couple buttons: one to run the game and another to reset the game.
Once the game button is clicked, it runs my JavaScript function, which includes a prompt() and a randomization of the choices. A winner is declared. However, I noticed all of my HTMl has dissapeared!
- Why and where did my content disappear?
- How can I make it so they remain while the user is playing the game and entering info into the prompt?
Code:
<!DOCTYPE html>
<html>
<title>Game: RPS</title>
<body>
<h1>JS Test Output</title>
<h3>Rock, Paper, Scissors: You vs Computer!</h3>
<button onclick="game()">Play Game</button>
<input type="button" value="Reload Page" onClick="window.location.reload()">
<script>
function game(){
var a = prompt("Rock, Paper, or Scissors?");
var b = ["Rock","Paper","Scissors"];
var rand = b[Math.floor(Math.random() * b.length)];
if (a== "Rock" && rand=="Paper") {
document.write("Computer Wins!");
}
else if (a=="Rock" && rand=="Scissors") {
document.write("Player 1 wins!");
}
else if (a=="Paper" && rand=="Rock") {
document.write("Player 1 wins!");
}
else if (a=="Paper" && rand=="Scissors") {
document.write("Computer wins!");
}
else if (a=="Scissors" && rand=="Rock") {
document.write("Computer wins!");
}
else if (a=="Scissors" && rand=="Paper") {
document.write("Player 1 wins!");
}
else if (a == rand) {
document.write("We have a tie!");
}
else {
document.write("That's not a choice! Computer wins!");
}
}
</script>
</body>
</html>
Thank you for reading, any contributions are most appreciated. This is my first post and I'm really interested in learning this to hopefully become a programmer some day (I plan on taking off time from work and possibly entering a bootcamp). I really enjoy trying/reading/learning programming (don't think I'm ready to learn computer science things like algorithms , data structures yet, or should i start now????)
Jon