Rock Paper Scissors JavaScript

Brah been staring at my code for a while please someone help me figure out what’s wrong.

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 ‘The game was a Tie’
}
if (userChoice === ‘rock’) {
if(computerChoice === ‘paper’) {
return ‘Sorry, Computer won!’;
} else {
return ‘Congratulations, you won!’
}
}
}
if(userChoice === ‘paper’) {
if(computerChoice === ‘scissors’) {
return ‘Sorry, Computer won!’;
} else {
return ‘Congratulations, you won!’
}
}
if(userChoice === ‘scissors’){
if(computerChoice === ‘rock’) {
return ‘Sorry, Computer won!’;
} else {
return ‘Congratulations, you won!’
}
}

Was this part of a function? userChoice was not defined.

In the determineWinner function, you have a } in the wrong place. [ link to previous post on this ]
As a result, the computer thinks that function ends before some of those if-statements (so the computer thinks these are outside the function).

Your code (formatted a bit):

const determineWinner = (userChoice, computerChoice) => {
  if(userChoice === computerChoice){
      return 'The game was a Tie'
  }
  if (userChoice === 'rock') {
    if(computerChoice === 'paper') {
        return 'Sorry, Computer won!';
    } else {
        return 'Congratulations, you won!'
    }
  }
}  // the } does not belong here (it ends the function)
  if(userChoice === 'paper') {
    if(computerChoice === 'scissors') {
        return 'Sorry, Computer won!';
    } else {
        return 'Congratulations, you won!'
    }
  }
  if(userChoice === 'scissors'){
    if(computerChoice === 'rock') {
        return 'Sorry, Computer won!';
    } else {
        return 'Congratulations, you won!'
    }
  }
// there should be a } here
1 Like

Hello, I am wondering the same question, my part of my code reads as (excerpts):

// Set variables
const UserChoice = getUserChoice(userInput);
const ComputerChoice = getComputerChoice();

/* — TESTING Output both choices — TESTING */
console.log('TEST user - ’ + UserChoice);
console.log('TEST computer - ’ + ComputerChoice);

------- snip _________

let determineWinner = (UserChoice, ComputerChoice) => {

/**** DRAW *** */
if (UserChoice === ComputerChoice) {
console.log('FUNCTION1 - ’ + UserChoice + ’ @ ’ + ComputerChoice);
return ‘Its a draw’;
}

_________ snip ____________________
The output I get is:

TEST user - rock
TEST computer - scissors
FUNCTION1 - undefined @ undefined
Its a draw

I don’t understand where (userChoice, computerChoice) comes from in the code listed earlier in the thread when it does not appear to be defined? And why it is output as undefined in my code when explicitly defined.
PS Sorry if my post is not great, I am finishing a night shift and won’t be able to sleep if I have not at least asked the question.

Be careful with the scope of each variable … the variable could be defined inside a function, but not defined outside (because the scope of a variable declared in a function would be limited to that function, it would be inaccessible outside that function, and you’d actually be trying to access a different variable [that has global scope] ).

Thanks for the reply.
I am still confused however.
With much code edited out my script reads:

const UserChoice = getUserChoice(userInput);
const ComputerChoice = getComputerChoice();

The output tests correctly for those 2 lines of code.

I am then trying to pass those two variables to this function:

let determineWinner = (UserChoice, ComputerChoice) => {
if (UserChoice === ComputerChoice) {
console.log('FUNCTION1 - ’ + UserChoice + ’ @ ’ + ComputerChoice);
}

But the output returned is:

FUNCTION1 - undefined @ undefined

The parameters are passed when creating the function and the output is created and sent to the console from within the function. I just can’t get my head around what I am doing wrong.
Thanks for any replies.

Since both variables are assigned function call return values, this would suggest that neither function is returning anything (except the default).

What I think I am doing (incorrect thinking obviously) is passing the global variables (UserChoice, ComputerChoice) to the function determineWinner which compares them (within the function) and outputs them to the console (from within the function). Purely for testing.
Should I get that to work I would then return the result of processing those global variables back to the main program and eliminate the console call within the determineWinner function.

The problem might be where you call the function, not inside the function itself.
What are you using as arguments are you using when you call the function?

Or post the rest of your code. The errors may be more visible then.
Use the </> button, and put the code between the ``` and ``` so that the code keeps its formatting.

Print all results at the end, not inside any functions. Return the values and accumulate them, then display the final outcome. Repeat, don’t do any printing inside your functions. Have them return values, only.

This post was flagged by the community and is temporarily hidden.