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()
Related
I'm doing this rock, paper, and scissors game, however, my last function it's not working properly, it won't execute that block of code, and what it's wrong with the variables in the last function, they cannot be called, I've tried to change the parameters, I also debugged the code and to the extent I notice just the last function it's not working properly
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput == "rock" || userInput == "paper" || userInput == "scissors") {
return userInput;
} else {
console.log('Enter a valid option')
};
};
function getComputerChoice(any) {
return Math.floor(Math.random() * any)
}
switch (getComputerChoice(3)) //userInput
{
case 0:
console.log('rock');
break;
case 1:
console.log('paper');
break;
case 2:
console.log('scissors');
break;
default:
console.log('Select a valid option')
}
function determineWinner(getUserChoice, getComputerChoice) {
if (getUserChoice === getComputerChoice) {
return 'The game was a tie'
}
if (getUserChoice === 'rock') {
if (getComputerChoice === 'paper') {
return 'The computer won!'
} else {
return 'You won'
}
}
if (getUserChoice === 'scissors') {
if (getComputerChoice === 'rock') {
return 'Computer won!'
} else {
return 'You won!'
}
}
}
const playGam = () => {
const userChoice = getUserChoice('scissors');
const computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
playGam();
As posted, getComputerChoice just does random math and multiplies by a (sometimes missing) param. Consider changing to return a random choice from the set of ['rock', 'paper', 'scissors']...
function getComputerChoice() {
const choices = ['rock', 'paper', 'scissors'];
return choices[Math.floor(Math.random() * choices.length)]
}
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput == "rock" || userInput == "paper" || userInput == "scissors") {
return userInput;
} else {
console.log('Enter a valid option')
};
};
function getComputerChoice() {
const choices = ['rock', 'paper', 'scissors'];
return choices[Math.floor(Math.random() * choices.length)]
}
switch (getComputerChoice(3)) //userInput
{
case 0:
console.log('rock');
break;
case 1:
console.log('paper');
break;
case 2:
console.log('scissors');
break;
default:
console.log('Select a valid option')
}
function determineWinner(getUserChoice, getComputerChoice) {
if (getUserChoice === getComputerChoice) {
return 'The game was a tie'
}
if (getUserChoice === 'rock') {
if (getComputerChoice === 'paper') {
return 'The computer won!'
} else {
return 'You won'
}
}
if (getUserChoice === 'scissors') {
if (getComputerChoice === 'rock') {
return 'Computer won!'
} else {
return 'You won!'
}
}
}
const playGam = () => {
const userChoice = getUserChoice('scissors');
const computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
playGam();
We could also improve the code in a few other ways...
// make this global
const choices = ['rock', 'paper', 'scissors'];
const beats = { rock: 'scissors', scissors: 'paper', paper: 'rock' };
// general purpose fn to validate
const validateChoice = choice => {
return choices.includes(choice.toLowerCase());
};
// a tightly specified thing to determine winner
const determineWinner(userChoice, computerChoice) {
if (computerChoice === userChoice) return 'tie';
return beats[userChoice] === computerChoice ? 'user win' : 'computer win';
}
You're not setting a variable value with your swtich statement. You need to return what's the computer's choice, so I've put your switch statement inside the getComputerChoice function and then it returns the output and you can use it in your playGam function. I've updated that as well to display the winner in every outcome.
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput == "rock" || userInput == "paper" || userInput == "scissors") {
return userInput;
} else {
console.log('Enter a valid option')
};
};
function getComputerChoice(any) {
const randomNo = Math.floor(Math.random() * any)
switch (randomNo) //userInput
{
case 0:
return 'scissors';
break;
case 1:
return 'paper';
break;
case 2:
return 'rock';
break;
default:
return 'Select a valid option';
}
}
function determineWinner(getUserChoice, getComputerChoice) {
if (getUserChoice === getComputerChoice) {
return 'The game was a tie'
}
switch (getUserChoice) {
case 'paper':
return getComputerChoice === 'scissors' ? 'The computer won!' : 'You won';
case 'rock' :
return getComputerChoice === 'scissors' ? 'The computer won!' : 'You won';
case 'scissors' :
return getComputerChoice === 'paper' ? 'You won' : 'The computer won!';
default:
return 'Something went wrong'
}
}
const playGam = () => {
const userChoice = getUserChoice('paper');
const computerChoice = getComputerChoice(3);
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
playGam();
Currently working on TOP rock paper scissors game and I'm having some trouble. The function playRound is showing up as undefined in the console. I'm not sure why as all my other function are working. Maybe i did something wrong in the other functions but I'm not sure. I feel like I'm so close yet so far from this thing working. Maybe it's just common beginner mistakes not sure. Any help would be appreciated.
function computerPlay() {
var pickRandom = ["Rock", "Paper", "Scissors"];
var randomMove = pickRandom[Math.floor(Math.random() * 3)];
if (randomMove == "Rock") {
randomMove.value = 0;
} else if (randomMove == "Paper") {
randomMove.value = 1;
} else if (randomMove == "Scissors") {
randomMove.value = 2;
}
return randomMove;
}
console.log(computerPlay());
var playerSelection = prompt("pick rock paper or scissors");
function userPlay() {
if (playerSelection == "rock") {
playerSelection.value = 0;
} else if (playerSelection == "paper") {
playerSelection.value = 1;
} else if (playerSelection == "scissors") {
playerSelection.value = 2;
}
return playerSelection;
}
console.log(userPlay());
function playRound(playerPick, computerSelection) {
if (playerPick == 0 && computerSelection == 2) {
alert("you win!!!");
} else if (playerPick == 0 && computerSelection == 1) {
alert("you lose hahahahaha!!!");
} else if (playerPick == computerSelection) {
alert("its a tie");
}
}
const playerPick = userPlay();
const computerSelection = computerPlay();
console.log(playRound(playerPick, computerSelection));
You shouldn't run it in console.log, as it doesn't have a return keyword, so it will return undefined. Just run it normally and it will be fine.
You are close. I cleaned it up a bit, added a function that maps variants of human input to a normalized value, and replaced the (incomplete) conditions in playRound with an object that captures all of the win conditions in an object (see beats)...
function normalizePlay(play) {
const variants = { r: 'rock', rock: 'rock', p: 'paper', paper: 'paper', s: 'scissors', scissors: 'scissors' };
return variants [play.toLowerCase()];
}
function computerPlay() {
var pickRandom = ["rock", "paper", "scissors"];
return pickRandom[Math.floor(Math.random() * 3)];
}
function playRound(human, computer) {
const beats = { rock: 'scissors', scissors: 'paper', paper: 'rock' }
let result;
if (human === computer) {
result = 'tie';
}
else if (beats[human] === computer) {
result = 'you win';
}
else result = 'computer wins';
console.log(`you picked ${human}, computer picked ${computer}... ${result}`)
}
const human = normalizePlay(prompt("pick rock paper or scissors (or r, p, s)"));
const computer = computerPlay();
playRound(human, computer);
function computerPlay() {
var pickRandom = ["Rock", "Paper", "Scissors"];
var randomMove = pickRandom[Math.floor(Math.random() * 3)];
if (randomMove == "Rock") {
return 0;
} else if (randomMove == "Paper") {
return 1;
} else if (randomMove == "Scissors") {
return 2;
}
}
var playerSelection = prompt("pick Rock, Paper or Scissors");
function userPlay() {
if (playerSelection == "Rock") {
return 0;
} else if (playerSelection == "Paper") {
return 1;
} else if (playerSelection == "Scissors") {
return 2;
}
}
function playRound(playerPick, computerSelection) {
console.log("player", playerPick)
console.log("computerSelection", computerSelection)
if (playerPick == 0 && computerSelection == 2) {
console.log("you win!!!");
} else if (playerPick == 0 && computerSelection == 1) {
console.log("you lose hahahahaha!!!");
} else if (playerPick == computerSelection) {
console.log("its a tie");
}
}
const playerPick = userPlay();
const computerSelection = computerPlay();
playRound(playerPick, computerSelection);
You you just needed to return the numbers from computerPlay and userPlay. Also, be careful with case sensitivity when using the prompt.
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!
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'));