Rock, Paper, or Scissors

Hello Guys,
after writing the project, i found this issue and i’m not able to understand why.


This is my code:

const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else {
  console.log('Error, please choose better');
}
};

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) => {
return 'Draw';
}
if (userChoice === 'rock') {
  if (computerChoice === 'paper') {
    return 'Computer Won';
  } else {
    return 'You Won';
  }
}
if (userChoice === 'paper') {
  if (computerChoice === 'scissors') {
    return 'The computer won';
  } else {
    return 'You won!';
  }
}
if (userChoice === 'scissors') {
  if (computerChoice === 'rock') {
    return 'The computer won';
  } else {
    return 'You won';
  }
}

console.log(determineWinner('paper', 'scissors')); 
console.log(determineWinner('paper', 'paper')); 
console.log(determineWinner('paper', 'rock')); 

```Hoping to understand why
Thank you!

userChoice variable/parameter has a local scope, userChoice only exists within the body of your function, which is currently one line:

// function declaration
const determineWinner = (userChoice, computerChoice) => {
return 'Draw'; // body of function
}

then outside/after the function, you tried to access a variable outside its designated scope

1 Like

Thank you stetim! Finally i had solved the issue :smiley: :smiley: