Rock, paper or scissors error

Please, I have been stuck by 2 days. This is my code:

// User Choice:
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’) {
console.log(User's choice is ${userInput});
return userInput;
} else {
console.log(‘Error: no one of the three options selected.’)
}
};
getUserChoice(‘rock’);

// Computer Choice:
const getComputerChoice = () => {
let computerInput = Math.floor(Math.random()*3);
switch (computerInput) {
case 0:
computerInput = ‘rock’;
break;
case 1:
computerInput = ‘paper’;
break;
case 2:
computerInput = ‘scissors’;
break;
}
console.log(Computer's choice is ${computerInput});
return computerInput;
}

getComputerChoice();

function determineWinner (userChoice,computerChoice) {
if (userChoice === computerChoice) {
return console.log(‘The game is a tie.’);
}
}

determineWinner (userInput, computerInput);
________________ :arrow_up: ReferenceError: userInput is not defined

I got that error in this point. Why? Isn’t ‘return’ of each function supposed to return the value of these variables in order to use them in other functions?

Ty for your support.

Well, that depends. return sends a value back to the caller. Your error says that userInput is undefined. If the interpreter made it to the next variable in that line, you’d get an error saying computerInput is undefined. Where in your code did you define either of these variables? Don’t forget the rules regarding scope.

Consider:

const foo = someParameter => {
  let whatever = someParameter;
  if(whatever == "Hey") {
    whatever += ", you!"; 
  } else {
    whatever += ", there.";
  }
  return whatever;
}

foo("Hey"); //foo is called, and the value assigned to whatever is returned to here, but then what?
console.log(whatever); //whatever is not defined in this scope

Output:

ReferenceError: whatever is not defined

Versus:

const foo = someParameter => {
  let whatever = someParameter;
  if(whatever == "Hey") {
    whatever += ", you!"; 
  } else {
    whatever += ", there.";
  }
  return whatever;
}

const message = foo("Hey"); //foo is called, the value assigned to whatever is returned and assigned to message
console.log(message);

Output:

Hey, you!

When we call a function, and it returns a value, we either need to use that value right away or assign it to a variable, otherwise, it is lost. In my example, if I wanted to just use the returned value right away, I could use the function call as an argument to another function like console.log() for example:

console.log(foo("Hello")); //would print: Hello, there.

Also, for future posts, please refer to How do I format code in my posts?

Ty for your time.

I don’t undersatnd. In this project, I cann’t call ‘determineWinner’ with an attribute like ‘rock’ assigned because this attribute is variable and it depends on each function above. For this reason I used ‘determineWinner(userInput, computerInput);’

In your function call, determineWinner(userInput, computerInput) you are passing two arguments, but you have not assigned values to either of them.

You have functions that return the values that you want to use as arguments, but you don’t do anything to make use of the return values. getComputerChoice() only calls the function. If you want to keep the return value for later use (you do), you’ll have to assign the value to a variable:

const computerChoice = getComputerChoice(); //this assigns the return value to a variable named computerChoice

Ok.
I changed my code and it works:

let computerOut=getComputerChoice();
let userOut=getUserChoice('rock');
determineWinner (userOut,computerOut);

Thanks a lot @midlindner

1 Like