I'm new to JavaScript and I want to create a rock, paper, scissors game from scratch.
My Game Plan:
Once the user clicks on an option (rock, paper, scissors), the game will display their score and the computer's score along with the result of the round. When the player's score reaches a certain point, they will be prompted to enter their name and it will be logged somewhere.
If anyone could provide me with some guidance on how to achieve this, I would greatly appreciate it as I am just starting out. You can check out my progress on JSFiddle where I have incorporated HTML and CSS.
The link to my work on JSFiddle: https://jsfiddle.net/y3qu7pzz/
Thank you!
This is the JS code:
function playgame(x){
var options = ["rock", "paper", "scissors"];
var playerChoice = x;
var randomNumber = Math.floor(Math.random() * options.length);
var computerChoice = options[randomNumber];
function determineWinner(){
if (playerChoice == computerChoice) {
$("#results").html("You selected " + playerChoice + " and the computer selected " + computerChoice + ". Game tied.<br />");
} else if ((playerChoice == "rock" && computerChoice == "paper") || (playerChoice == "scissors" && computerChoice == "rock") || (playerChoice == "paper" && computerChoice == "scissors")) {
$("#results").html("You selected " + playerChoice + " and the computer selected " + computerChoice + ". You lost. <br />");
} else if ((playerChoice == "rock" && computerChoice == "scissors") || (playerChoice == "scissors" && computerChoice == "paper") || (playerChoice == "paper" && computerChoice == "rock")) {
$("#results").html("You selected " + playerChoice + " and the computer selected " + computerChoice + ". You won. <br />");
} else {
alert("Please enter rock, paper, or scissors");
}
}
determineWinner();
}
$(".start").click(function (){
var choice = $(this).attr("id");
playgame(choice);
})