Help for a beginner in JavaScript

Hello all,

I’m new to all of this so thank you in advance for your time and patience! I just started my journey on Codecademy. I’m working on the Javascript game “Rock, Paper, Scissors” and am really having trouble.

Somehow, my code is just not working:

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
   return userInput;} else {
     console.log('Error');
   }
}
//Jetzt ist der Computer an der Reihe
const getComputerChoice = () => {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
  }
}

// Jetzt wird programmiert, wer der Gewinner ist
const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'the game is tied!';
    }
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You have won!';
      }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'The computer won!';
    } else {
      return 'You have won!';
    }
  }
   if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You have won!';
    }
  }

const playGame = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice ();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);

console.log(determineWinner(userChoice, computerChoice));
};

playGame();


Maybe you can help me! :slight_smile:
Thank you so much!

The code concept is fine you just have to watch out for syntax with the brackets.

3 Likes

Hello, @joshuafriedrich17128, and welcome to the forums!

When I copied and ran your code, I received the following error: SyntaxError: Illegal return statement pointing to this line:

That could be a bit tricky to figure out. Consider why a return would be illegal to use here. return can only be used inside a function. Is the return in question still inside the determineWinner function?

3 Likes

Thank you so much! It was not inside the determineWinner function. Basically, I just had to delete the } above if (userChoice === 'rock') and place a missing one on the bottom to close the determineWinner function.

:slight_smile:

1 Like

You’re welcome. Nice work!