Rock,Paper,Scissors round counter in JavaScript - javascript

I'm trying to make a console version of rock paper scissors. The game is working for now but I can't run the for loop, what I want to do is at the end of each round, the console will say what round we are in and tell who won the current round. It will be the Best of 5 and the winner will be determined in round 5.
Since I'm a beginner I couldn't try too much. I know my code is not clean and complicated because I proceed by trying and I'm also new to Stackoverflow, if there is something missing in the topic, I apologize in advance for those.
// PLAYER CODES
let playerSelection = prompt('Do you choose Rock, Paper or Scissors?')
if (playerSelection === 'Rock' || playerSelection === 'Paper' || playerSelection === 'Scissors') {
console.log("You chose " + `${playerSelection}`);
} else {
console.log('Error! Pick Rock, Paper or Scissors')
}
// COMPUTER CODES
let computerSelection = getComputerChoice();
function getComputerChoice() {
let choices = ['Rock', 'Paper', 'Scissors'];
let random = choices[Math.floor(Math.random() * 3)];
return random;
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection){
return ('Draw')
} else if ((playerSelection === 'Rock') && (computerSelection === 'Scissors')) {
return ('Player Wins! Rock beats Scissors.')
} else if ((playerSelection === 'Scissors') && (computerSelection === 'Paper')) {
return ('Player Wins! Scissors beats Paper.')
} else if ((playerSelection === 'Paper') && (computerSelection === 'Rock')) {
return ('Player Wins! Paper beats Rock.')
} else if ((computerSelection === 'Scissors') && (playerSelection === 'Paper')) {
return ('Computer Wins! Scissors beats Paper.')
} else if ((computerSelection === 'Paper') && (playerSelection === 'Rock')) {
return ('Computer Wins! Paper beats Rock.')
} else if ((computerSelection === 'Rock') && (playerSelection === 'Scissors')) {
return ('Computer Wins! Rock beats Scissors.')
}
}
function game() {
for (let i = 1; i < 6; i++){
playRound(playerSelection, computerSelection);
if (i == 1)
return ('Round 1')
else if (i == 2)
return ('Round 2')
else if (i == 3)
return ('Round 3')
else if (i == 4)
return ('Round 4')
else if (i == 5)
return ('Round 5')
}
}
console.log("Computer chose " + `${computerSelection}`);
console.log(playRound(playerSelection, computerSelection));
console.log(game(playRound));

You have to put the sections to get the player and computer choices inside the loop. I fixed some other problems, too.
// PLAYER CODES
function getPlayerChoice () {
let playerSelection = prompt('Do you choose Rock, Paper or Scissors?')
if (playerSelection === 'Rock' || playerSelection === 'Paper' || playerSelection === 'Scissors') {
console.log("You chose " + `${playerSelection}`);
return playerSelection
} else {
console.log('Error! Pick Rock, Paper or Scissors')
return getPlayerChoice()
}
}
// COMPUTER CODES
function getComputerChoice() {
let choices = ['Rock', 'Paper', 'Scissors'];
let random = choices[Math.floor(Math.random() * 3)];
return random;
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection){
return ('Draw')
} else if ((playerSelection === 'Rock') && (computerSelection === 'Scissors')) {
return ('Player Wins! Rock beats Scissors.')
} else if ((playerSelection === 'Scissors') && (computerSelection === 'Paper')) {
return ('Player Wins! Scissors beats Paper.')
} else if ((playerSelection === 'Paper') && (computerSelection === 'Rock')) {
return ('Player Wins! Paper beats Rock.')
} else if ((computerSelection === 'Scissors') && (playerSelection === 'Paper')) {
return ('Computer Wins! Scissors beats Paper.')
} else if ((computerSelection === 'Paper') && (playerSelection === 'Rock')) {
return ('Computer Wins! Paper beats Rock.')
} else if ((computerSelection === 'Rock') && (playerSelection === 'Scissors')) {
return ('Computer Wins! Rock beats Scissors.')
}
}
function game() {
for (let i = 1; i < 6; i++){
console.log('Round ' + i)
let playerSelection = getPlayerChoice();
let computerSelection = getComputerChoice();
console.log("Computer chose " + `${computerSelection}`);
console.log(playRound(playerSelection, computerSelection));
}
}
game()

Related

How to set the result in a loop. Building the game: rock, paper, scissors

I am working on a game of rock, paper, scissors in the course "The Odin Project." Unfortunately, I've been at a standstill for several days because I can't figure out how to set the outcome of the duel. I am uploading my code below. I am a beginner so please understand :) I would appreciate your help.
const choice = ['rock', 'paper', 'scissors']
function getComputerChoice() {
const computerPlay = choice[Math.floor(Math.random() * choice.length)]
return computerPlay
}
function playRound(playerSelection, computerSelection) {
let YouLose = `You lose ${computerSelection} beats ${playerSelection.toLowerCase()}!`
let YouWin = `You win ${playerSelection.toLowerCase()} beats ${computerSelection}!`
if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'paper') {
return YouLose
score ++
} else if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'rock') {
return YouWin
}
else if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'scissors') {
return YouLose
}
else if (playerSelection.toLowerCase() === 'scissors' && computerSelection === 'paper') {
return YouWin
} else if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'scissors') {
return YouLose
}
else if (playerSelection.toLowerCase() === 'scissors' && computerSelection === 'rock') {
return YouLose
}else if(playerSelection.toLowerCase() === computerSelection) {
return "Tie!"
} else {
return "Error"
}
}
// function checkWinner() {
// if (playRound(playerSelection, computerSelection) === YouWin) {
// playerScore++;
// }else if (playRound(playerSelection, computerSelection) === YouLose) {
// computerScore++;
// }
// }
function game() {
console.log("Let's play the game!");
let playerScore = 0
let computerScore = 0
for (let i = 0; i < 5; i++) {
const playerSelection = prompt('What do you choose, rock, scissors or paper?')
const computerSelection = getComputerChoice()
playRound(playerSelection, computerSelection)
if (playRound(playerSelection.toLowerCase(), computerSelection) === YouWin) {
playerScore++;
}else if (playRound(playerSelection.toLowerCase(), computerSelection) === YouLose) {
computerScore++;
}
console.log(playRound(playerSelection, computerSelection));
console.log('-------------------');
console.log(playerScore);
console.log(computerScore);
}
}
console.log(game());
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="script.js"></script>
<title>Document</title>
</head>
<body>
<script>
// function computerPlay() {
// let game = ['rock', 'paper' , 'scissors'];
// let randomPlay = Math.floor(Math.random()*game.length);
// return game[randomPlay]
// }
// let player = 0
// let computer = 0
// let round = 0
// function playRound(playerSelection, computerSelection) {
// if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'paper') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// } else if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'rock') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// }
// else if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'scissors') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// }
// else if (playerSelection.toLowerCase() === 'scissors' && computerSelection === 'paper') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// } else if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'scissors') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// }
// else if (playerSelection.toLowerCase() === 'scissors' && computerSelection === 'rock') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// } else if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'rock') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// } else if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'paper') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// } else if (playerSelection.toLowerCase() === 'scissors' && computerSelection === 'scissors') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// } else {
// return "Erorr"
// }
// console.log(computer);
// }
// function game () {
// for (let i = 0; i < 5; i++) {
// playerSelection = prompt('What do you choose, rock, scissors or paper?');
// computerSelection = computerPlay();
// console.log(playRound(playerSelection, computerSelection));
// }
// }
// // console.log(computerSelection);
// // console.log(playerSelection);
// console.log(game());
</script>
</body>
</html>
https://github.com/RadekLewandowski/rock-paper-scissors/blob/6f60d9721a3f7e7835e5e9254cb26fcb766bedc2/script.js
i just fixed your ideas:
const choice = ['rock', 'paper', 'scissors']
function getComputerChoice() {
const computerPlay = choice[Math.floor(Math.random() * choice.length)]
return computerPlay
}
function playRound(playerSelection, computerSelection) {
if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'paper') {
return 'YouLose'
score ++
} else if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'rock') {
return 'YouWin'
}
else if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'scissors') {
return 'YouLose'
}
else if (playerSelection.toLowerCase() === 'scissors' && computerSelection === 'paper') {
return 'YouWin'
} else if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'scissors') {
return 'YouLose'
}
else if (playerSelection.toLowerCase() === 'scissors' && computerSelection === 'rock') {
return 'YouLose'
}else if(playerSelection.toLowerCase() === computerSelection) {
return "Tie!"
} else {
return "Error"
}
}
function game() {
console.log("Let's play the game!");
let playerScore = 0
let computerScore = 0
for (let i = 0; i < 5; i++) {
const playerSelection = prompt('What do you choose, rock, scissors or paper?')
const computerSelection = getComputerChoice()
const outcome = playRound(playerSelection, computerSelection)
if (outcome === 'YouWin')
{
alert(`You win ${playerSelection.toLowerCase()} beats ${computerSelection}!`)
playerScore++;
}
else if (outcome === 'YouLose')
{
computerScore++;
alert(`You lose ${computerSelection} beats ${playerSelection.toLowerCase()}!`)
}
console.log(playRound(playerSelection, computerSelection));
console.log('-------------------');
console.log(playerScore);
console.log(computerScore);
}
}
console.log(game());
Applied lots of fixes, simplified the code which determines whether you win or lose and ended up with this:
const choice = ['rock', 'paper', 'scissors']
function getComputerChoice() {
const computerPlay = choice[Math.floor(Math.random() * choice.length)]
return computerPlay
}
let YouLose;
let YouWin;
function playRound(playerSelection, computerSelection) {
YouLose = `You lose ${computerSelection} beats ${playerSelection.toLowerCase()}!`;
YouWin = `You win ${playerSelection.toLowerCase()} beats ${computerSelection}!`;
let diff = ((choice.indexOf(playerSelection.toLowerCase()) - choice.indexOf(computerSelection.toLowerCase())) + 3) % 3;
switch (diff) {
case 1: return YouWin;
case 2: return YouLose;
default: return "Tie!";
}
}
// function checkWinner() {
// if (playRound(playerSelection, computerSelection) === YouWin) {
// playerScore++;
// }else if (playRound(playerSelection, computerSelection) === YouLose) {
// computerScore++;
// }
// }
function game() {
console.log("Let's play the game!");
let playerScore = 0
let computerScore = 0
function round(index) {
let playerSelection = prompt('What do you choose, rock, scissors or paper?');
while (choice.indexOf(playerSelection ? playerSelection.toLowerCase() : "") < 0) {
playerSelection = prompt('Invalid choice. What do you choose, rock, scissors or paper?');
}
const computerSelection = getComputerChoice();
let result = playRound(playerSelection.toLowerCase(), computerSelection.toLowerCase());
if (result === YouWin) {
playerScore++;
}else if (result === YouLose) {
computerScore++;
}
alert(result);
console.log('-------------------');
console.log('Player: ' + playerScore);
console.log('Computer: ' + computerScore);
setTimeout(function() {
round(index + 1);
}, 1000);
}
round(0);
}
console.log(game());

Loop prompt if a value is empty / wrong?

I'm doing a simple rock, paper, exercise on console (from The Odin Project) and I am stucked in some point. I want the prompt that selects rock, paper or scissors to keep showing if the user input is empty or wrong (if is a different string from the variable choices) and I'm not getting the reuslt I want, the alert shows, yes, but it shows whether if you input a correct value, a wrong value or if you left it empty and the prompt also finishes after the loop for ends which I only want to finish if the input is "rock", "paper" or "scissors".
Can someone enlight me here? I'm just blank.
const choices = ["rock", "paper", "scissors"];
let playerScore = 0;
let computerScore = 0;
let playerName = "";
function getComputerChoice() {
let random = choices[Math.floor(Math.random() * choices.length)];
console.log(random);
return random;
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return "It's a tie";
} else if (playerSelection === "rock") {
if (computerSelection === "scissors") {
playerScore++;
return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
} else if (computerSelection === "paper") {
computerScore++;
return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
}
} else if (playerSelection === "paper") {
if (computerSelection === "rock") {
playerScore++;
return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
} else if (computerSelection === "scissors") {
computerScore++;
return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
}
} else if (playerSelection === "scissors") {
if (computerSelection === "paper") {
playerScore++;
return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
} else if (computerSelection === "rock") {
computerScore++;
return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
}
}
}
function playerChoice() {
const userChoice = prompt("Rock, paper, scissors").toLowerCase();
if (userChoice == null || userChoice !== choices) {
alert("Empty or wrong choice!");
} else if (choices.includes(userChoice)) {
return userChoice;
}
}
function getPlayerName() {
playerName = prompt("What is your name?");
return playerName;
}
function game () {
getPlayerName();
console.log(`Welcome ${playerName}`)
for (i = 0; i < 3; i++) {
const computerSelection = getComputerChoice();
const playerSelection = playerChoice();
console.log(playRound(playerSelection, computerSelection));
}
if (playerScore > computerScore) {
return "Game over! Player wins!";
} else if (playerScore < computerScore) {
return "Game over! Player loses!";
} else {
return "Game over, draw!";
}
}
console.log(game());
Thank you very much.
The problem is you're checking whether the input is the same as the array choices. using the include function here will check that the user's choice is in the array
const choices = ["rock", "paper", "scissors"];
let playerScore = 0;
let computerScore = 0;
let playerName = "";
function getComputerChoice() {
let random = choices[Math.floor(Math.random() * choices.length)];
console.log(random);
return random;
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return "It's a tie";
} else if (playerSelection === "rock") {
if (computerSelection === "scissors") {
playerScore++;
return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
} else if (computerSelection === "paper") {
computerScore++;
return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
}
} else if (playerSelection === "paper") {
if (computerSelection === "rock") {
playerScore++;
return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
} else if (computerSelection === "scissors") {
computerScore++;
return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
}
} else if (playerSelection === "scissors") {
if (computerSelection === "paper") {
playerScore++;
return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
} else if (computerSelection === "rock") {
computerScore++;
return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
}
}
}
function playerChoice() {
const userChoice = prompt("Rock, paper, scissors").toLowerCase();
console.log(choices.includes(userChoice))
if (userChoice == null || !choices.includes(userChoice)) {
alert("Empty or wrong choice!");
} else if (choices.includes(userChoice)) {
return userChoice;
}
}
function getPlayerName() {
playerName = prompt("What is your name?");
return playerName;
}
function game () {
getPlayerName();
console.log(`Welcome ${playerName}`)
for (i = 0; i < 3; i++) {
const computerSelection = getComputerChoice();
const playerSelection = playerChoice();
console.log(playRound(playerSelection, computerSelection));
}
if (playerScore > computerScore) {
return "Game over! Player wins!";
} else if (playerScore < computerScore) {
return "Game over! Player loses!";
} else {
return "Game over, draw!";
}
}
console.log(game());
Edit: Also you might want to change this line as a null input won't have the attribute .toLowerCase()
const userChoice = prompt("Rock, paper, scissors").toLowerCase();

stuck on the if on Rock, paper, scissors

I am new to coding I have to do this rock paper scissors but can't find my error it still show me the else result each time.
Any help are welcome
// Get the computer choice
function getComputerChoice() {
"use strict";
const computerPlay = Math.floor(Math.random()*3);
if (computerPlay === 1){
return("rock")
}
else if(computerPlay === 2){
return ("paper")
}
else {
return ("scissors")
}
}
// Get the user choice
function getUserChoice(){
prompt("Please enter 'rock', 'paper', 'scissors': ").toLowerCase();
}
// Play a round
function playRound(){
let computerSelection = getComputerChoice();
let playerSelection = getUserChoice();
if (computerSelection === playerSelection){
console.log("Game is a Tie")
}
else if (computerSelection == 'paper' && playerSelection == 'rock'){
console.log("Computer Win")
}
else if (computerSelection == 'rock' && playerSelection == 'scissors'){
console.log("Computer Win")
}
else if (computerSelection == 'scissors' && playerSelection == 'paper'){
console.log("Computer Win")
}
else {
console.log("User Win")
}
}
// Play Game function
function playGame(){
for (let i = 0; i < 5; i++){
playRound()
}
}
playGame()
I try the if seems it miss something to run correctly.
Solution
You've missed the return keyword from the getUserChoice method.
function getUserChoice(){
return prompt("Please enter 'rock', 'paper', 'scissors': ").toLowerCase();
}

How to compare the ID of a clicked button to another string?

I am trying to compare a clicked button's ID to a random string output from another function and log the result in the console (it's a rock, paper, scissors game); Code depicted below
buttons.forEach((button) => {
button.addEventListener("click", () => {
console.log(playRound(event.srcElement.id));
});
});
function playRound(playerSelection) {
const computerSelection = computerPlay;
if (playerSelection === 'rock' && computerSelection === 'rock') {
return 'Tie Game!';
} else if (playerSelection === 'rock' && computerSelection === 'paper') {
return 'Paper beats rock! You lose!';
} else if (playerSelection === 'rock' && computerSelection === 'scissors') {
return 'Rock beats scissors! You win!';
} else if (playerSelection === 'paper' && computerSelection === 'rock') {
return 'Paper beats rock! You win!';
} else if (playerSelection === 'paper' && computerSelection === 'paper') {
return 'Tie Game!';
} else if (playerSelection === 'paper' && computerSelection === 'scissors') {
return 'Scissors beats paper! You lose!';
} else if (playerSelection === 'scissors' && computerSelection === 'rock') {
return 'Rock beats Scissors! You lose!';
} else if (playerSelection === 'scissors' && computerSelection === 'paper') {
return 'Scissors beats paper! You win!';
} else if (playerSelection === 'scissors' && computerSelection === 'scissors') {
return 'Tie Game!';
}
}
function computerPlay() {
const play = (Math.floor(Math.random() * 3));
if (play === 0) {
return 'rock';
}
else if (play === 1) {
return 'paper';
}
else if (play === 2) {
return 'scissors';
}
}
I know this code is a bit inefficient but my problem is that the playRound() function returns undefined when I execute this. I've tried logging the button's ID alone, which worked, I've tried passing the button's ID as a parameter to a dummy function, which worked, and I tried logging the computerPlay() output to make sure it was returning a string (it was). I can't tell why my playRound() function is unable to compare the two strings given and return a result string.

JavaScript - Rock Paper Scissors in console

I'm having problems getting my simple Rock Paper Scissors game to display the winner of each game when the user chooses anything either than Rock. How do I use multiple if...else statements so that my declareWinner function works when the user chooses Scissors or Paper?
let choices = ["rock", "paper", "scissors"];
// GETTING USER INPUT
const userInput = prompt("Do you choose rock, paper or scissors?").toLocaleLowerCase();
if(userInput ==="paper" || userInput ==="rock" || userInput ==="scissors"){
console.log("You chose " + `${userInput}`);
} else {
console.log("Error! Try again!");
}
// GETTING COMPUTER INPUT
const computerInput = choices[Math.floor(Math.random() * 3)];
console.log("Computer chose" + ` ${computerInput}`);
if (computerInput <= 0.33) {
computerInput = "rock";
}
if (computerInput >= 0.67) {
computerInput = "paper";
}
if (computerInput >= 0.66) {
computerInput = "scissors";
}
// DECLARE WINNER
// User Chooses Rock
function declareWinner(userInput, computerInput) {
if(userInput === "rock" && computerInput === 'paper') {
console.log ('You win! Rock beats paper!');
} else if (userInput === 'rock' && computerInput === 'rock') {
console.log ('Its a tie!');
} else if (userInput === 'rock' && computerInput === 'scissors') {
console.log ('You win! Rock beats scissors!');
// User Chooses Paper
} else if(userInput === "paper" && computerInput === 'rock') {
console.log ('You win! Rock beats paper!');
} else if (userInput === 'paper' && computerInput === 'paper') {
console.log ('Its a tie!');
} else if (userInput === 'paper' && computerInput === 'scissors') {
console.log ('You lose! Scissors beats paper!');
// User Chooses Scissors
} else if(userInput === "scissors" && computerInput === 'rock') {
console.log ('You lose! Rock beats scissors!');
} else if (userInput === 'scissors' && computerInput === 'paper') {
console.log ('You win! Scissors beats paper!');
} else {
console.log ('It is a tie!');
}
}
declareWinner();
There were two problems with the code:-
You are doing choices[Math.floor(Math.random() * 3)]; which results in a random value from choices array, so there is no need for multiple if-else statements that were after it.
You were missing the declareWinner function declaration.
Also, You are missing some conditions in declareWinner function, like userInput === 'paper' && computerInput === 'rock', userInput === 'rock' && computerInput === 'scissors' etc.
const choices = ["rock", "paper", "scissors"];
// GETTING USER INPUT
const userInput = prompt("Do you choose rock, paper or scissors?");
if (userInput === "paper" || userInput === "rock" || userInput === "scissors") {
console.log("You chose " + `${userInput}`);
} else {
console.log("Error! Try again!");
}
// GETTING COMPUTER INPUT
let computerInput = choices[Math.floor(Math.random()*3)];
console.log("Computer chose" + ` ${computerInput}`);
// DECLARE WINNER
const winner = declareWinner(userInput, computerInput);
function declareWinner(userInput, computerInput) {
if (userInput === 'rock' && computerInput === 'paper') {
console.log('You lose! Rock beats Paper');
} else if (userInput === 'scissors' && computerInput === 'paper') {
console.log('You win! Scissors beats Paper');
} else {
console.log('You tie!');
}
}
Tip:- You can use an object to choose the winner in declareWinner function.
const choices = ["rock", "paper", "scissors"];
// GETTING USER INPUT
const userInput = prompt("Do you choose rock, paper or scissors?");
if (userInput === "paper" || userInput === "rock" || userInput === "scissors") {
console.log("You chose " + `${userInput}`);
} else {
console.log("Error! Try again!");
}
// GETTING COMPUTER INPUT
let computerInput = choices[Math.floor(Math.random() * 3)];
console.log("Computer chose" + ` ${computerInput}`);
// DECLARE WINNER
const winner = declareWinner(userInput, computerInput);
function declareWinner(userInput, computerInput) {
let win = {
rock: "scissors",
paper: "rock",
scissors: "paper"
};
if (win[userInput] === computerInput) {
console.log(`You won! ${userInput} beats ${computerInput}`);
} else if (win[computerInput] === userInput) {
console.log(`You lost! ${computerInput} beats ${userInput}`);
} else console.log("Tie")
}
You can't change a const value. Use let instead:
let computerInput = choices[Math.floor(Math.random() * 3)];

Categories