So I am working on some code to build a simple application in Javascript where the computer has a random number assigned to rock, paper, scissors and choose randomly and then has a userchoice as well. The code is not running not sure why.
I have tried adding semicolon to the end of the if statements that are inside the main if statement in the determineWinner function.
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
return userInput;
}else {
console.log('Error, you must type rock, paper, or scissors');
}
}
function getComputerChoice(){
var randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
function determineWinner(userChoice, computerChoice){
if(userChoice === computerChoice){
return 'This game is a tie!'
} else if(userChoice === 'rock'){
if(computerChoice === 'paper'){
return 'The computer has won!';
}else{
return 'The user has won';
}
} else if(userChoice === 'paper'){
if(computerChoice === 'scissors'){
return 'The computer has won!';
}else(computerChoice === 'rock');{
return 'The user has won!';
}
}else(userChoice === 'scissors');{
if(computerChoice === 'rock'){
return 'The computer has won!';
}else(computerChoice === 'paper');{
return 'The user has won!';
}
}
}
}
console.log(determineWinner('paper', 'scissors'));
When running the console.log at the end of the script it should display that the computer has won.
Since all of your if cases simply return, you don't need to use else since the if-case will always return. Like this:
function determineWinner(userChoice, computerChoice){
if (userChoice === computerChoice) {
return 'This game is a tie!'
}
if (userChoice === 'rock') {
if(computerChoice === 'paper') {
return 'The computer has won!';
}
return 'The user has won';
}
if (userChoice === 'paper') {
if(computerChoice === 'scissors'){
return 'The computer has won!';
}
return 'The user has won!';
}
if (userChoice === 'scissors') {
if(computerChoice === 'rock') {
return 'The computer has won!';
}
return 'The user has won!';
}
}
Another way is to just return at the end of the function with the answer like this:
function determineWinner(userChoice, computerChoice){
const results = [
'This game is a tie!',
'The computer has won!',
'The user has won!'
];
let winner;
if (userChoice === computerChoice) winner = 0;
else if (userChoice === 'rock') {
winner = computerChoice === 'paper' ? 1 : 2;
}
else if (userChoice === 'paper') {
winner = computerChoice === 'scissors' ? 1 : 2;
}
else if (userChoice === 'scissors') {
winner = computerChoice === 'rock' ? 1 : 2;
}
return results[winner];
}
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
return userInput;
}else {
console.log('Error, you must type rock, paper, or scissors');
}
}
function getComputerChoice(){
var randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
function determineWinner(userChoice, computerChoice){
if(userChoice === computerChoice){
return 'This game is a tie!'
} else if(userChoice === 'rock'){
if(computerChoice === 'paper'){
return 'The computer has won!';
}else if(computerChoice === 'scissors'){
return 'The user has won';
}
} else if(userChoice === 'paper'){
if(computerChoice === 'scissors'){
return 'The computer has won!';
}else if(computerChoice === 'rock'){
return 'The user has won!';
}
}else if(userChoice === 'scissors'){
if(computerChoice === 'rock'){
return 'The computer has won!';
}else if(computerChoice === 'paper'){
return 'The user has won!';
}
}
}
console.log(determineWinner('paper', 'scissors'));
Related
The first function getComputerChoice() returns a random - rock, paper, or scissor. However, in the playRound function when the computer' choice and user's choice are compared, it shows incorrect answers for a few of them. For example, when I choose rock and computer chooses paper, I win, which is wrong.
I tried using if else and switch statements but faced the same problem. It would be great if someone could help me out.
let computerSelection = getComputerChoice();
let playerSelection = prompt("rock, paper, or scissor?").toLowerCase();
function getComputerChoice(){
const randomNumber = Math.floor(Math.random()*3);
if(randomNumber === 0){
choice = "rock";
} else if(randomNumber === 1){
choice = "paper";
} else if(randomNumber === 2){
choice = "scissor";
} return choice;
}
function playRound(playerSelection, computerSelection){
let result = '';
if(computerSelection === playerSelection){
result = "It's a tie";
} else if(computerSelection === 'paper' && playerSelection === 'rock'){
result = "you lose";
} else if(computerSelection === 'scissor' && playerSelection === 'rock'){
result = "you win";
} else if ((computerSelection === 'rock') && (playerSelection === 'paper')){
result = "You win";
} else if((computerSelection === 'scissor') && (playerSelection === 'paper')){
result = "you lose";
} else if ((computerSelection === 'paper') && (playerSelection === 'scissor') ){
result = "You win";
} else if((computerSelection === 'rock') && (playerSelection === 'scissor')){
result = "You lose";
}
return result;
};
alert("The computer chose: " + getComputerChoice());
alert("That means " + playRound(playerSelection, computerSelection));
In your first statement, you choose the computer selection. But in your second to last statement, you call getComputerChoice() which makes a DIFFERENT selection. Change the next to the last line to
alert("The computer chose: " + computerSelection);
Try adding this debug line that I have put in, and then look at the console in the browser. You will see what the computer really chose
function getComputerChoice(){
const randomNumber = Math.floor(Math.random()*3);
if(randomNumber === 0){
choice = "rock";
} else if(randomNumber === 1){
choice = "paper";
} else if(randomNumber === 2){
choice = "scissor";
}
// debug line
console.log('randomly chose', choice);
return choice;
}
ChatGPT?
This code will run but it has a couple of problems:
In the getComputerChoice function, the variable choice is declared without const, let, or var. This means that it's a global variable and could cause issues in the future.
The line alert("The computer chose: " + getComputerChoice()); is calling the getComputerChoice function twice, but it should only be called once and stored in the computerSelection variable.
The result of the playRound function is not being logged to the console or displayed in any other way, it's only being returned.
To resolve these problems, you could modify the code as follows:
let computerSelection = getComputerChoice();
let playerSelection = prompt("rock, paper, or scissor?").toLowerCase();
function getComputerChoice(){
let choice = '';
const randomNumber = Math.floor(Math.random() * 3);
if(randomNumber === 0){
choice = "rock";
} else if(randomNumber === 1){
choice = "paper";
} else if(randomNumber === 2){
choice = "scissor";
}
return choice;
}
function playRound(playerSelection, computerSelection){
let result = '';
if(computerSelection === playerSelection){
result = "It's a tie";
} else if(computerSelection === 'paper' && playerSelection === 'rock'){
result = "you lose";
} else if(computerSelection === 'scissor' && playerSelection === 'rock'){
result = "you win";
} else if ((computerSelection === 'rock') && (playerSelection === 'paper')){
result = "You win";
} else if((computerSelection === 'scissor') && (playerSelection === 'paper')){
result = "you lose";
} else if ((computerSelection === 'paper') && (playerSelection === 'scissor') ){
result = "You win";
} else if((computerSelection === 'rock') && (playerSelection === 'scissor')){
result = "You lose";
}
return result;
};
console.log("The computer chose: " + computerSelection);
console.log("That means " + playRound(playerSelection, computerSelection));
Your Code is not consistent : here is the answer:
let computerSelection = getComputerChoice();
let playerSelection = prompt("rock, paper, or scissor?").toLowerCase();
function getComputerChoice(){
const randomNumber = Math.floor(Math.random() * 3);
let choice;
if(randomNumber === 0){
choice = "rock";
} else if(randomNumber === 1){
choice = "paper";
} else if(randomNumber === 2){
choice = "scissors";
}
return choice;
}
function playRound(playerSelection, computerSelection){
let result = '';
if(computerSelection === playerSelection){
result = "It's a tie";
} else if(computerSelection === 'paper' && playerSelection === 'rock'){
result = "You lose";
} else if(computerSelection === 'scissors' && playerSelection === 'rock'){
result = "You win";
} else if ((computerSelection === 'rock') && (playerSelection === 'paper')){
result = "You win";
} else if((computerSelection === 'scissors') && (playerSelection === 'paper')){
result = "You lose";
} else if ((computerSelection === 'paper') && (playerSelection === 'scissors') ){
result = "You lose";
} else if((computerSelection === 'rock') && (playerSelection === 'scissors')){
result = "You win";
}
return result;
};
let result = playRound(playerSelection, computerSelection);
alert("The computer chose: " + computerSelection);
alert("That means " + result);
Note:
scissor has been corrected to scissors.
Consistent use of capitalization for "You win" and "You lose".
The result ofplayRound() function has been stored in a variable
result before using it in the alert
.
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput
} else {
console.log('Error!')
}
}
const getcomputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3)
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'Tie'
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'computer won'
} else {
return 'user won'
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'computer won'
} else {
return 'user won'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'computer won'
} else {
return 'user won'
}
}
}
const playGame = () => {
var userChoice = getUserChoice()
console.log(userChoice)
var computerChoice = getcomputerChoice()
console.log(computerChoice)
console.log(determineWinner(userChoice, computerChoice))
}
console.log(determineWinner())
So I wanted to do a project with JS, was following codeacadem's course on the subject. But for some reason I am always getting a tie which I do not understand why. Can anyone please help me? I thought this is the correct code and every single object was working properly.
UPDATE:
After giving a parameter to the userchoice (getUserChoice('scissors')) I got this error: userInput = userInput.toLowerCase();
TypeError: Cannot read properties of undefined (reading 'toLowerCase')
Initializing playgame didnt solve the issue
I've corrected your code with a few things:
As #James has commented, getUserChoice() expects a parameter for the choice of the user but none is given at playGame().
As #Alon Eitan has commented, console.log(determineWinner()) doesn't play the game at all as it just calls determineWinner() with the two parameters undefined. And actually that's why Tie is always returned as undefined == undefined.
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput
} else {
console.log('Error!')
}
}
const getcomputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3)
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'Tie'
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'computer won'
} else {
return 'user won'
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'computer won'
} else {
return 'user won'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'computer won'
} else {
return 'user won'
}
}
}
const playGame = () => {
var userChoice = getUserChoice("rock")
console.log(userChoice)
var computerChoice = getcomputerChoice()
console.log(computerChoice)
console.log(determineWinner(userChoice, computerChoice))
}
playGame()
I'm learning Javascript and I make a rock, scissors, paper project. I already make it work with the sample code but now I want to use a function expression to use its value in other functions. When I load the code it gave me undefined and I don't know why. I assign all the values.
Here is my code.
const userChoice = userInput => {
//userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
return userInput;
} else {
console.log('Error!');
}
}
//console.log(getUserChoice('Fork'));
const computerChoice = function() {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors'
}
};
//console.log(getComputerChoice());
function determineWinner(userChoice, computerChoice) {
console.log()
if (userChoice === computerChoice) {
return 'The game is a tie!'
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'paper') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'bomb') {
return 'You won!';
}
}
const playGame = () => {
console.log('You threw: ' + userChoice('rock'));
console.log('The computer threw: ' + computerChoice());
console.log(determineWinner(userChoice, computerChoice));
}
playGame();
Please help me.
You're passing the function userChoice as a param, you should store the result of calling the function userChoice and pass the result as a param in the function determineWinner
const userChoice = userInput => {
//userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
return userInput;
} else {
console.log('Error!');
}
}
//console.log(getUserChoice('Fork'));
const computerChoice = function() {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors'
}
};
//console.log(getComputerChoice());
function determineWinner(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return 'The game is a tie!'
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'paper') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'bomb') {
return 'You won!';
}
}
const playGame = () => {
let uc = userChoice('rock');
console.log('You threw: ' + uc);
console.log('The computer threw: ' + computerChoice());
console.log(determineWinner(uc, computerChoice));
}
playGame();
You should assign the outputs of userChoice and computerChoice to variables before passing them to determineWinner
const userChoice = userInput => {
//userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
return userInput;
} else {
console.log('Error!');
}
}
//console.log(getUserChoice('Fork'));
const computerChoice = function() {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors'
}
};
//console.log(getComputerChoice());
function determineWinner(userChoice, computerChoice) {
console.log()
if (userChoice === computerChoice) {
return 'The game is a tie!'
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'paper') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'bomb') {
return 'You won!';
}
}
const playGame = () => {
const userChose = userChoice('rock')
const computerChose = computerChoice()
console.log('You threw: ' + userChose);
console.log('The computer threw: ' + computerChose);
console.log(determineWinner(userChose, computerChose));
}
playGame();
Try This
const userChoice = userInput => {
//userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
return userInput;
} else {
console.log('Error!');
}
}
//console.log(getUserChoice('Fork'));
const computerChoice = function() {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors'
}
};
//console.log(getComputerChoice());
function determineWinner(userChoice, computerChoice) {
console.log()
if (userChoice === computerChoice) {
return 'The game is a tie!'
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'paper') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'bomb') {
return 'You won!';
}
}
const playGame = () => {
console.log('You threw: ' + userChoice('rock'));
console.log('The computer threw: ' + computerChoice());
console.log(determineWinner(userChoice('rock'), computerChoice()));
}
playGame();
just a few mistakes.
You need to store the values into variables.
const userChoice = userInput => {
//userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
return userInput;
} else {
console.log('Error!');
}
}
//console.log(getUserChoice('Fork'));
const computerChoice = function() {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors'
}
};
//console.log(getComputerChoice());
function determineWinner(userChoice, computerChoice) {
console.log()
if (userChoice === computerChoice) {
return 'The game is a tie!'
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'paper') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'bomb') {
return 'You won!';
}
}
const playGame = () => {
const threw = {
computer: null,
user: null
}
threw.user = userChoice("rock")
threw.computer = computerChoice()
console.log('You threw: ' + threw.user);
console.log('The computer threw: ' + threw.computer);
console.log(determineWinner(userChoice(threw.user), threw.computer));
}
playGame();
**I am trying to create rock paper scissors in java script and The below is the code I am executing, It is showing "The match is draw! play again" everytime. What goes wrong?
First taking the user input and checking the correctness. Generating the computer input by Math.random and assigning them RPS based on the output. Finally comparing both to determine the winner.**
const getUserChoice = function(userInput){
userInput = userInput.toLowerCase();
if (userInput === 'rock'|| userInput ==='paper'|| userInput ==='scissors'){
return userInput;
}else {
console.log('Error');
}
}
userChoice = getUserChoice('rock');
console.log(getUserChoice('rock'));
const getComputerChoice = function(){
randomNumber = Math.floor(Math.random()*3);
switch(randomNumber) {
case 0:
return 'rock';
break;
case 1:
return 'paper';
break;
case 2:
return 'scissors';
break;
default:
return 'error';
}
}
computerChoice = getComputerChoice();
console.log(getComputerChoice());
const determineWinner = function(userChoice, computerChoice){
if (userChoice === computerChoice){
return 'The match is draw! Play again.';
}
if (userChoice === 'rock'){
if (computerChoice === 'paper'){
return 'you lost';
}else{
return 'you won';
}
}
if (userChoice === 'paper'){
if (computerChoice === 'rock'){
return 'you won';
}else{
return 'you lost';
}
}
if (userChoice === 'scissors'){
if (computerChoice === 'rock'){
return 'you lost';
}else{
return 'you won';
}
}
}
console.log(determineWinner());**strong text**
On your bottom line, you are not passing any parameters through.
Change it to:
console.log(determineWinner(userChoice, computerChoice));**strong text**
Currently, the first line of your determineWinner function is checking if null is equal to null (which it is), so is returning a draw.
Here you go. Checkout the below answer:
const getUserChoice = function(userInput){
userInput = userInput.toLowerCase();
if (userInput === 'rock'|| userInput ==='paper'|| userInput ==='scissors'){
return userInput;
}else {
console.log('Error');
}
}
const userChoice = getUserChoice('rock');
const getComputerChoice = function(){
const randomNumber = Math.floor(Math.random() * 3);
switch(randomNumber) {
case 0:
return 'rock';
break;
case 1:
return 'paper';
break;
case 2:
return 'scissors';
break;
default:
return 'error';
}
}
const computerChoice = getComputerChoice();
const determineWinner = function(userChoice, computerChoice){
console.log('user choice: ', userChoice);
console.log('computer choice: ', computerChoice);
if (userChoice === computerChoice){
return 'The match is draw! Play again.';
}
if (userChoice === 'rock'){
if (computerChoice === 'paper'){
return 'you lost';
} else {
return 'you won';
}
}
if (userChoice === 'paper'){
if (computerChoice === 'rock'){
return 'you won';
}else{
return 'you lost';
}
}
if (userChoice === 'scissors'){
if (computerChoice === 'rock'){
return 'you lost';
}else{
return 'you won';
}
}
}
console.log(determineWinner(userChoice, computerChoice));
It's simply because you are not passing the arguments to the determineWinner function.
Change the last line to:
determineWinner(userChoice,computerChoice);
Currently it's considering both choices as null and returning you the output as draw!
How do i stop the function from executing if the userChoice is null or a word apart from rock,paper or scissors.
I've tried to use return but i couldn't get it to work.
Any help is appreciated.
Thanks
JS Fiddle Link = http://jsfiddle.net/Renay/d9bea2ra/1/
var userChoice = prompt('Do you choose rock, paper or scissors?');
var compChoice = Math.random();
if (compChoice <= 0.34) {
compChoice = 'rock';
} else if (compChoice <= 0.67) {
compChoice = 'paper';
} else {
compChoice = 'scissors';
}
function compare() {
if (userChoice == compChoice) {
console.log( 'The result is a tie!');
} else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper') ) {
console.log( 'You win!');
} else {
console.log('You lose!');
}
if (userChoice === null) {
console.log('Please select an option');
} else if (userChoice !== 'rock'&&'paper'&&'scissors') {
console.log('Please select rock, paper or scissors');
}
}
console.log('Your choice = ' + userChoice);
console.log('Computer Choice = ' + compChoice);
compare();
The condition in your if statement is wrong. It should be:
if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors')
An expression of the form e1 && e2 && e3 && ... evaluates to last eN sub-expression if all of them truthy. So your test was equivalent to:
if (userChoice !== 'scissors')
You should put that check before displaying the result of the game, and return from the function then. So it should be:
function compare() {
if (userChoice === null) {
console.log('Please select an option');
return;
} else if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') {
console.log('Please select rock, paper or scissors');
return;
}
if (userChoice == compChoice) {
console.log( 'The result is a tie!');
} else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper') ) {
console.log( 'You win!');
} else {
console.log('You lose!');
}
}
Just do return;
function compare() {
if(!userChoice)
return;
// (...) more code
}
A return call exits the function. But note that you can only use return inside a function
If i understand correctly, you want to return to the beginning of the code. You should wrap the entire thing in a function, then call that function again:
function rockPaperScissors() {
var userChoice = prompt('Do you choose rock, paper or scissors?');
var compChoice = Math.random();
if (compChoice <= 0.34) {
compChoice = 'rock';
} else if (compChoice <= 0.67) {
compChoice = 'paper';
} else {
compChoice = 'scissors';
}
function compare() {
if (userChoice == compChoice) {
console.log( 'The result is a tie!');
} else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper') ) {
console.log( 'You win!');
} else {
console.log('You lose!');
}
if (userChoice === null) {
console.log('Please select an option');
rockPaperScissors();
} else if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') { // fixed
console.log('Please select rock, paper or scissors');
rockPaperScissors();
}
}
console.log('Your choice = ' + userChoice);
console.log('Computer Choice = ' + compChoice);
compare();
}
rockPaperScissors();
You also had an error with checking if it isn't one of rock, paper, or scissors, so i fixed that. (It seems Barmar got to it before me.)
I updated each if/else part of your function to exit when it match if/else condition with return false. In addition I changed the if (userChoice === null) check to if (userChoice === "") because it looks like that prompt('Do you choose rock, paper or scissors?'); returns an empty string when the user doesn't enter any value. Fiddle Also, I just updated the beginning of the function to work accordingly to **Barmar'**s suggestion (he got it right first time around).
function compare() {
if (userChoice === "") {
console.log('Please select an option');
return false;
} else if ((userChoice !== 'rock') && (userChoice !=='paper') && (userChoice !=='scissors')) {
console.log('Please select rock, paper or scissors');
return false;
}
if (userChoice == compChoice) {
console.log('The result is a tie!');
return false;
} else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper')) {
console.log('You win!');
return false;
} else {
console.log('You lose!');
return false;
}
}