Not printing the winner in Rock,Paper,Scissor game. For some reason I cannot get my function DeclareWinner to return any of the strings I have created.
After I did a short test where I put a string outside of my if/else statements I managed to get a print. It seems that my returns are stuck in the local scope and does therefore not get printed.
var weapon = "rock"
function Userchoice(weapon){
if (weapon === "rock" || weapon === "paper" || weapon === "scissor") {
return weapon
}
else {
return "Invalid Choice";
}
}
function Computerchoice(){
var Number = Math.floor(Math.random() * 3);
if (Number === 0) {
return "rock";
}
else if (Number === 1) {
return "paper";
}
else if (Number === 2) {
return "scissor";
}
}
function DeclareWinner(Userchoice, Computerchoice){
if (Userchoice === Computerchoice){
return "Tiebreak";
}
else if (Userchoice === "rock" && Computerchoice === "scissor"){
return "User wins";
}
else if (Userchoice === "rock" && Computerchoice === "paper"){
return "Computer wins";
}
else if (Userchoice === "paper" && Computerchoice === "rock"){
return "User wins";
}
else if (Userchoice === "paper" && Computerchoice === "scissor"){
return "Computer wins";
}
else if (Userchoice === "scissor" && Computerchoice === "rock"){
return "Computer wins";
}
else if (Userchoice === "scissor" && Computerchoice === "paper"){
return "User wins";
}
}
console.log(Userchoice(weapon));
console.log(Computerchoice());
console.log(DeclareWinner(Userchoice, Computerchoice));
The expectation of console.log(DeclareWinner(Userchoice, Computerchoice)); is to print who is the winner.
(ps. I know there are several other rock paper scissor games who are more technical, I just wanted to try and create my own without specific guidance)
Thanks in advance.
You are not passing the correct arguments to your function. Userchoice and Computerchoice are both references to the corresponding functions and do not represent the (string) results of your previous function calls as you might expect.
You can easily verify this by adding a console.log(Userchoice), which prints:
function Userchoice(weapon){if(weapon==="rock"||weapon==="paper"||weapon==="scissor"){return weapon;}else{return"Invalid Choice";}}
You want to store the results of the function calls before printing and pass them to DeclareWinner.
Relevant part:
// Your unchanged code
// ...
let userchoice = Userchoice(weapon);
let computerchoice = Computerchoice();
console.log(userchoice);
console.log(computerchoice);
console.log(DeclareWinner(userchoice, computerchoice));
As a side note: Variable and method names usually start with a lower case letter.
Related
i'm learning javascript and currently making a rock, paper, scissor game using only javascript. the game will have 1 round mode and 3 rounds mode but as i finished to code the 1 round mode i found out having problem display result, whoever win the game it display "the game is tie" and can't find where s the mistake, can anybody help me?
// Player choice
var getPlayerChoice = function() {
var playerChoice = prompt("Choose rock, paper, or scissors");
while (playerChoice !== 'rock' && playerChoice !== 'paper' && playerChoice !== 'scissors') {
if (playerChoice === null) {
break;
}
playerChoice = prompt("Invalid ! you have to choose rock, paper, or scissors");
}
return playerChoice;
}
// Computer Choice
var getComputerChoice = function () {
var randomNum = Math.random();
if ( randomNum < 0.3333 ) {
return "rock";
} else if ( randomNum > 0.3333 && randomNum < 0.6666 ) {
return "scissors";
} else {
return "paper";
}
}
// Winner Function
var getWinner = function (playerChoice, computerChoice) {
if (computerChoice === playerChoice) {
return "The Game is Tie";
} else if (computerChoice === "paper") {
if (playerChoice === "scissors") {
return "player win";
} else if (playerChoice === "rock") {
return "computer win";
}
} else if (computerChoice === "rock") {
if (playerChoice === "scissors") {
return "computer win";
} else if (playerChoice === "paper") {
return "player win";
}
} else if (computerChoice === "scissors") {
if (playerChoice === "rock") {
return "player win";
} else if (playerChoice === "paper") {
return "computer win";
}
}
}
// Single game mode
var singleRound = function() {
var playerChoice = getPlayerChoice();
if (playerChoice === null) {
return;
}
var computerChoice = getComputerChoice();
var winner = getWinner(playerChoice, computerChoice);
var message = " You chose: " + playerChoice + "\n Computer chose: " + computerChoice;
if (winner === "player") {
alert(message + "\nYou won!");
} else if (winner === "computer") {
alert(message + "\nYou lost!");
} else {
alert(message + "\nThe Game is Tie");
}
return winner;
}
var mode = prompt("Welcome!\n \nplease insert 1 for single round mode\n2 for 3 rounds mode");
if (mode === '1') {
singleRound();
} else if (mode === '2') {
threeRoundsMode();
}
Your getWinner() function returns player win or computer win, but your code that calls it is looking for return values of player or computer.
Because the code never gets what it's looking for it defaults to `'The Game is Tie'
This is happening because of a mistake in the singleRound() function. if (winner === "player") { should be if (winner === "player win") { and similarly the if (winner === "computer") { should say if (winner === "computer win") { so that the text being compared matches. Right now, it is comparing "player" and "player win", then "computer" and "computer win" so then the else clause is reached regardless of actual game outcome.
I'm currently writing a Rock, Paper, Scissors game in Javascript, and for some reason, no matter what the player input is, I always get a "draw" result. I've been trying to figure it out for the last hour but no dice. Any help is really appreciated. I've put my code below.
let computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
let playerPrompt = prompt("Rock, paper, or scissors?")
let playerChoice = String(playerPrompt).toLowerCase
function playRound(playerChoice, computerChoice) {
if (playerChoice === computerChoice) {
return "Draw!"
} else if (playerChoice === "rock" && computerChoice === "scissors") {
return "Player wins!"
} else if (playerChoice === "paper" && computerChoice === "rock") {
return "Player wins!"
} else if (playerChoice === "scissors" && computerChoice === "paper") {
return "Player wins!"
} else {
return "Computer wins!"
}
}
let results = playRound()
console.log(results)```
If I'm not mistaken, you're not passing any arguments to playRound(), it should probably be:
let results = playRound(playerChoice, computerChoice)
Edit: As mentionned by Quentin (and Alon Eitan) it is not the only problem:
let playerChoice = String(playerPrompt).toLowerCase
actually assigns the function String.toLowerCase to playerChoice, if you want the lower case value of playerPrompt the syntax should be
let playerChoice = playerPrompt.toLowerCase()
or directly
let playerChoice = prompt("Rock, paper, or scissors?").toLowerCase()
The error is toLowerCase instead of toLowerCase(), you missed the parenthesis. Try running this snippet, it works
let computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
let playerPrompt = prompt("Rock, paper, or scissors?")
let playerChoice = String(playerPrompt).toLowerCase()
function playRound(playerChoice, computerChoice) {
if (playerChoice === computerChoice) {
return "Draw!"
} else if (playerChoice === "rock" && computerChoice === "scissors") {
return "Player wins!"
} else if (playerChoice === "paper" && computerChoice === "rock") {
return "Player wins!"
} else if (playerChoice === "scissors" && computerChoice === "paper") {
return "Player wins!"
} else {
return "Computer wins!"
}
}
let results = playRound(playerChoice, computerChoice)
console.log(results)
I made a basic "Rock, paper or scissors" game. I have a few doubts/problems about this project.
On my browser the message of who wins isn't displayed. Such as "Computer wins". What I get is the following:
Computer: Paper
You: rock
And my code is:
let userChoice = prompt("Do you choose Rock, Paper or Scissors?");
let computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "Rock";
} else if(computerChoice <= 0.67) {
computerChoice = "Paper";
} else {
computerChoice = "Scissors";
}
let compare = function(choice1, choice2) {
if (choice1 === choice2) {
document.getElementById('printwo').innerHTML = 'The result is a tie!';
}
else if (choice1 === "Rock"){
if (choice2 ==="Scissors") {
document.getElementById('printwo').innerHTML = 'Rock wins';
}
else {
document.getElementById('printwo').innerHTML = 'Paper wins';
}
}
else if (choice1 === 'Paper') {
if (choice2 === 'Rock') {
document.getElementById('printwo').innerHTML = 'Paper wins';
}
else {
document.getElementById('printwo').innerHTML = 'Scissors wins';
}
}
else if (choice1 === 'Scissors') {
if (choice2 ==='Rock') {
document.getElementById('printwo').innerHTML = 'Rock wins';
}
else {
document.getElementById('printwo').innerHTML = 'Scissors wins';
}
}
};
document.getElementById("print").innerHTML = "Computer: " + computerChoice + "</br>" + "You: " + userChoice;
compare(userChoice, computerChoice);
I also wanted to add an else statement to give the message "Please insert a valid response" in case the user doesn't choose any of the 3 options. But where should I place it inside the function? I tried putting it at the end but then it always displayed the message, no matter what I wrote.
Instead of so many else if statements, why can't I write this game using a function which contains many lines of code such as this one:
else if(userChoice === 'Paper' && computerChoice === 'Paper') {
return ('It\'s a tie!'); }
Last but not least, why doesn't it work to use return (instead of document.getElementById()) inside the function and then call the function inside of document.getElementById()
Thanks in advance.
I also wanted to add an else statement to give the message "Please insert a valid response" in case the user doesn't choose any of the 3 options. But where should I place it inside the function? I tried putting it at the end but then it always displayed the message, no matter what I wrote.
This doesn't belong in the compare function.
It will be best when you get the user input.
You might want to repeatedly ask the user until you get a valid input, for example:
const choices = ['Rock', 'Paper', 'Scissors'];
var userChoice;
while (true) {
prompt("Do you choose Rock, Paper or Scissors?");
if (choices.indexOf(userChoice) != -1) break;
console.log('Invalid choice!');
}
By the way, defining the choices also lets you simplify the way you set the computer's choice:
let computerChoice = choices[Math.floor(Math.random() * choices.length)];
Instead of so many else if statements, why can't I write this game using a function which contains many lines of code such as this one:
else if(userChoice === 'Paper' && computerChoice === 'Paper') {
return ('It\'s a tie!'); }
You don't actually have that exact line, thanks to the choice1 === choice2 condition.
In any case, there's nothing fundamentally wrong with having all those if-else-ifs.
You could use a different approach, for example an object oriented one,
where each object would know what other choice beats it and what doesn't,
but it's not so obvious that such approach would be significantly better.
Last but not least, why doesn't it work to use return (instead of document.getElementById()) inside the function and then call the function inside of document.getElementById()
Indeed, it would be better to remove all the repetitive document.getElementById('printwo').innerHTML = from inside the compare function.
You could write like this instead:
let compare = function(choice1, choice2) {
if (choice1 === choice2) {
return 'The result is a tie!';
} else if (choice1 === "Rock") {
if (choice2 === "Scissors") {
return 'Rock wins';
} else {
return 'Paper wins';
}
} else if (choice1 === 'Paper') {
if (choice2 === 'Rock') {
return 'Paper wins';
} else {
return 'Scissors wins';
}
} else {
if (choice2 === 'Rock') {
return 'Rock wins';
} else {
return 'Scissors wins';
}
}
};
document.getElementById('printwo').innerHTML = compare(userChoice, computerChoice);
A more compact writing style is possible with using the ternary operator ?::
if (choice1 === choice2) {
return 'The result is a tie!';
} else if (choice1 === "Rock") {
return choice2 === "Scissors" ? 'Rock wins' : 'Paper wins';
} else if (choice1 === 'Paper') {
return choice2 === "Rock" ? 'Paper wins' : 'Scissors wins';
} else {
return choice2 === "Rock" ? 'Rock wins' : 'Scissors wins';
}
Here's an alternative data-driven approach:
const rps = {
rock: {
paper: 'Paper wins',
scissors: 'Rock wins'
},
paper: {
rock: 'Paper wins',
scissors: 'Scissors wins'
},
scissors: {
rock: 'Rock wins',
paper: 'Scissors wins'
}
};
let compare = function(choice1, choice2) {
if (choice1 === choice2) {
return 'The result is a tie!';
}
return rps[choice1.toLowerCase()][choice2.toLowerCase()];
};
Ok so this should be fairly easy to answer but for some reason im having an issue.( could be because im very new to programming) I've created a rock,papers,scissors game against the CPU. Now i want to ask the user if they want to play again and if they answer Y then it should go through the loop each time. If they type "N" then the game will end. The main issue im having is that once you type in Y to play again it just gives you the result from the last game. Any pointers would help.
Here is what i have :
var userChoice = "";
var userChoice = prompt("Choose rock, paper, or scissors");
var playagain = "Y";
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
choice1 = userChoice;
choice2 = computerChoice;
while (playagain == "Y") {
function compare(choice1, choice2) {
if (choice1 == choice2) {
return ("It's a tie!");
}
if (choice1 == "rock") {
if (choice2 == "scissors") {
return ("You win!");
} else {
return ("computer wins!");
}
}
if (choice1 == "paper") {
if (choice2 == "rock") {
return ("you win!");
} else {
return ("computer wins!");
}
}
if (choice1 == "scissors") {
if (choice2 == "rock") {
return ("computer wins!");
} else {
return ("you win!");
}
}
}
document.write(compare(choice1, choice2));
document.write("<br>");
playagain = prompt("Do you want to play again, Y or N");
userChoice = prompt("Choose rock, paper, or scissors");
}
The only thing you need is to move all game logics under the while loop:
function compare(choice1, choice2) {
if (choice1 == choice2) {
return ("It's a tie!");
}
if (choice1 == "rock") {
if (choice2 == "scissors") {
return ("You win!");
} else {
return ("computer wins!");
}
}
if (choice1 == "paper") {
if (choice2 == "rock") {
return ("you win!");
} else {
return ("computer wins!");
}
}
if (choice1 == "scissors") {
if (choice2 == "rock") {
return ("computer wins!");
} else {
return ("you win!");
}
}
}
}
var playagain = "Y";
while (playagain == "Y") {
var userChoice = prompt("Choose rock, paper, or scissors");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
document.write(compare(userChoice, computerChoice));
document.write("<br>");
playagain = prompt("Do you want to play again, Y or N");
}
I have:
moved everything except the declaration of playagain into the loop
removed choice1 and choice2 local variables, as they are redundant
removed prompt in the end of the loop, since we have one in the beginning
removed the declaration of an empty userChoice in the beginning
by the advice of #LiYinKing, moved the function out of the loop
Here is the working JSFiddle demo.
Inside of the while loop you need to include your user prompt and new computer generated random number. The problem is your loop is calculating the same random number and user input over and over.
It's hard to explain exactly what's wrong because the code is difficult to follow.
I don't know how you expect the code that sets the computer's choice to run again after the code executes to the bottom of the script.
Declaring named functions in a loop doesn't make sense. Declare them at the top. Then there's nothing left in the body of the loop. Fishy...
What you need is a function that runs the game and a while loop calling that function.
I am on a phone, this is untested
function runGame() {
var userChoice = prompt("Choose rock, paper, or scissors");
var computerChoice= Math.random();
if(computerChoice < 0.34){
computerChoice = "rock";
} else if(computerChoice<=0.67){
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
if(userChoice==computerChoice){
return "It's a tie!";
}
if(userChoice=="rock"){
if(computerChoice=="scissors"){
return "You win!";
} else{
return "computer wins!";
}
}
if(userChoice=="paper"){
if(computerChoice=="rock"){
return "you win!";
} else{
return "computer wins!";
}
}
if(userChoice=="scissors"){
if(computerChoice=="rock"){
return "computer wins!" ;
} else{
return "you win!" ;
}
}
}
alert(runGame());
while ( prompt("Do you want to play again, Y or N") == "Y") {
alert(runGame());
}
Later with experience and knowledge you can write the script like this :
function compare(choice1, choice2) {
if (choice1 == choice2) {
return ("It's a tie!");
}
var lose = {
"rock": "scissors",
"paper": "rock",
"scissors": "paper"
};
return (lose[choice1] == choice2) ? "You win!" : "computer wins!";
}
(function() {
var CHOICE = ["rock", "paper", "scissors"];
var userChoice = "";
var userChoice, computerChoice, playagain, i = 0;
do {
document.write("Ground " + i + " :<br>");
computerChoice = CHOICE[Math.floor(Math.random() * 3)];
do {
userChoice = (""+prompt("Choose rock, paper, or scissors")).toLowerCase();
} while (CHOICE.indexOf(userChoice) === -1);
document.write(userChoice + " vs " + computerChoice + "<br>");
document.write(compare(userChoice, computerChoice) + "<br>");
playagain = (""+prompt("Do you want to play again, Y or N")).toUpperCase();
i++;
} while (["Y", "YES"].indexOf(playagain) !== -1);
});
Explanation :
I add toLowerCase() function to replace "Rock" to "rock", and I add toUpperCase() to replace "y" to "Y".
In this circumstance do { /* script */ } while (bool) is more suitable.
With :
do {
userChoice = (""+prompt("Choose rock, paper, or scissors")).toLowerCase();
} while (CHOICE.indexOf(userChoice) === -1);
You wait a valid answer, three word only : rock or paper or scissors. When userChoice equals "rock" indexOf return 0 ; equals "paper" indexOf return 1...
This syntax :
return (lose[choice1] == choice2) ? "You win!" : "computer wins!";
Is :
if (lose[choice1] == choice2)
return "You win!";
else
return "computer wins!"
JavaScript ternary operator example with functions
Ive made a small Rock paper scissor game with javascript. There are two things that I would like to do to make the game a little better.
1) If the player gives an answer other than rock, paper or scissors for a prompt saying "Please pick between one of the three options: rock, paper or scissors"
I have implemented something like this but it only goes one run. I would like to have the prompt come up until one of the three answers is given
2) I would like to have the game start running code from the top again if it is a tie. How could I get the program to start from the top again?
Here is the code
var userChoice = prompt("Do you choose rock, paper or scissors?");
if (userChoice === "rock")
{}
else if (userChoice === "paper"){}
else if (userChoice === "scissors"){}
else {
userChoice = prompt ("Please pick between one of the three options: rock, paper or scissors");
}
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log( "The computer's choice is" + " " + computerChoice);
console.log("Your choice was " + userChoice);
var compare = function(choice1 , choice2)
{
if (choice1 === choice2)
{
return "The result is a tie!";
}
if (choice1 === "rock")
{
if (choice2 === "scissors")
{
return "rock wins";
}
else
{
return "paper wins";
}
}
if (choice1 === "paper")
{
if (choice2 === "rock")
{
return "paper wins";
}
else
{
return "scissors wins";
}
}
if (choice1 === "scissors")
{
if (choice2 === "rock")
{
return "rock wins";
}
else
{
return "scissors wins";
}
}
};
compare(userChoice, computerChoice);
What you are doing in this program is only 'Conditional statement' checking. There is another construct to Structured Programming that is called 'Loops'
Generally it is humanly possible to copy-paste a segment of code 1000s of times and maybe even millions but not infinite. So for such cases programmers use Loops that start with an initial state, execute the same body of code as long as a given condition holds and follows a state change.
You can use a while loop in this case as it is the simplest structure for such scenarios. It only takes the 'condition' for which it much keep executing the body of code. The structure of while loop is as such.
while ( condition ) {
<the code that you want it to keep executing>
}
If we break your program down into various parts there are mainly 2 parts to it.
1. Take the input
2. Check if the input is valid or not. If not take the input again.
From this you can easily see what should be the condition for your loop.
"While the input is not valid"
So it's like
while ( the input is not valid / the userChoice is not rock or paper or scissors ) {
take the input
}
To make your loop infinite you can use
while ( true ) {
take the input
}
To break out of this infinite loop, you use "break;" anywhere inside it. So a 'break;' inside an if statement will get out of this loop like this
while ( true ) {
if ( condition ) {
break;
}
}
So, if we follow your style of condition checks we get.
var userChoice = prompt("Do you choose rock, paper or scissors?");
while ( true ) {
if (userChoice === "rock")
{break;}
else if (userChoice === "paper"){break;}
else if (userChoice === "scissors"){break;}
else {
userChoice = prompt ("Please pick between one of the three options: rock, paper or scissors");
}
}
But using 'breaks' is a bad programming practice. So why don't we take the advantage of while's 'condition'?
var userChoice = prompt("Do you choose rock, paper or scissors?");
while (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors") {
userChoice = prompt("Please pick between one of the three options: rock, paper or scissors");
}
And since you want the Game to go on forever, we can put the whole game inside a loop right? But that will totally take your control off your browser and you can't do anything while that loop is waiting for your response. So we keep a way out for ourselves? Why don't we add another condition that, if the user types "EXIT" the game stops?
while ( true ) {
var userChoice = prompt("Do you choose rock, paper or scissors?");
while (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors" && userChoice !== "EXIT") {
userChoice = prompt("Please pick between one of the three options: rock, paper or scissors");
}
if (userChoice === "EXIT") {
console.log("Thanks for playing :)");
break;
}
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log( "The computer's choice is" + " " + computerChoice);
console.log("Your choice was " + userChoice);
var compare = function(choice1 , choice2)
{
if (choice1 === choice2)
{
return "The result is a tie!";
}
if (choice1 === "rock")
{
if (choice2 === "scissors")
{
return "rock wins";
}
else
{
return "paper wins";
}
}
if (choice1 === "paper")
{
if (choice2 === "rock")
{
return "paper wins";
}
else
{
return "scissors wins";
}
}
if (choice1 === "scissors")
{
if (choice2 === "rock")
{
return "rock wins";
}
else
{
return "scissors wins";
}
}
};
compare(userChoice, computerChoice);
}
Now, you can make this much easier to Modify later (for you and others) by breaking your program down into functions just the way you did for the Compare part. Then taking the decision on the userInput will be much more robust process.
function take_user_input() {
var userChoice = prompt("Do you choose rock, paper or scissors?");
while (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors" && userChoice !== "EXIT") {
userChoice = prompt("Please pick between one of the three options: rock, paper or scissors");
}
return userChoice;
}
function play(userChoice) {
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log( "The computer's choice is" + " " + computerChoice);
console.log("Your choice was " + userChoice);
var compare = function(choice1 , choice2)
{
if (choice1 === choice2)
{
return "The result is a tie!";
}
if (choice1 === "rock")
{
if (choice2 === "scissors")
{
return "rock wins";
}
else
{
return "paper wins";
}
}
if (choice1 === "paper")
{
if (choice2 === "rock")
{
return "paper wins";
}
else
{
return "scissors wins";
}
}
if (choice1 === "scissors")
{
if (choice2 === "rock")
{
return "rock wins";
}
else
{
return "scissors wins";
}
}
};
compare(userChoice, computerChoice);
}
while ( true ) {
var userChoice = take_user_input();
if (userChoice === "EXIT") {
console.log("Thanks for playing :)");
break;
} else {
play(userChoice);
}
}
And after that, you can study more on reading DOM elements and modifying them using jQuery / modifying the HTML by Javascript. :) But that brings a totally new topic. But I'd suggest you to look that way. Nobody would love to play Rock Paper Scissors using Console Log when they can do that using Graphical User Interface.
You could wrap the game in a function, which you then call at any point. Maybe put the initial prompt in its own function, if its valid, call the game function.
function getUserInput()
{
// put prompt and validation logic here, then call when its a tie
// You might try consolidating the validation logic with regular expression:
// This returns an array of matches if only rock, paper or scissors was entered.
userChoice.match(/(rock|paper|scissors)/)
if(userChoice)
{
runGameLogic();
}
}