Enhancing RPS


Hello fellow coders! I am currently going through the JavaScript course, and I have managed to learn how to code the Rock, Paper, Scissors game without referencing the course now, which I am excited about. I'm looking to try to add more functionality to the game. I went ahead and added a simple validation for userChoice using the code below.

I have two questions. Firstly, is this the best way to validate userChoice? With the knowledge I have up to this point, this seems like the simplest way to do it, but there could be a better way.

Secondly, I would like to somehow add a counter to the game that would track the user's wins and losses. It would also be nice to allow the user to continually play without going through the initial prompt I have at the beginning of the game. What is the best way to go about doing this?

I may be better off waiting. I know the next lesson involves learning loops, and I'm thinking adding functionality like this will involve using loops, though I could be wrong. At any rate, if any of you have any suggestions about the best way to add that functionality, I'm all ears!

Thanks for your help! Love the community here.

// Introduction and rules confirmation
confirm("Welcome to the most incredible Rock, Paper, Scissors experience you will ever have inside your web browser. I certainly hope you enjoy playing the game as much as I enjoyed making it. Have fun! Press OK to continue!")

// Sets a variable for userChoice
var userChoice = prompt("Do you choose rock, paper, or scissors?")

//Validating user choice

if (userChoice === "rock") {
    userChoice = "rock";
else if (userChoice === "paper") {
    userChoice = "paper";
else if (userChoice === "scissors") {
    userChoice = "scissors";
else {
    userChoice = prompt("Whoops! It appears you have made an invalid selection. Please choose rock, paper, or scissors");


Loops indeed. And the assignments are redundant. Don't do anything you wouldn't do yourself


Thanks, good to know I'm on the right track mentally.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.