Questions - JS project Rock, papper or scissors

Hello everone,
I have met some problems when I try to do this project.
when I run the code, it show that userChoice has been declared.
If I remove const for userChoice and computerChoice in the last function like following, it works fine, but it’s diffrent than in the hint. I am wondering how does it happen?

const playGame = (userChoice, computerChoice) =>{ userChoice = getUserChoice('scissors'); computerChoice = getComputerChoice(); console.log('you threw '+userChoice); console.log('computer threw '+computerChoice); console.log(determineWinner(userChoice, computerChoice)); }

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput==='paper' || userInput === 'scissors'){
  return userInput;}
  else if (userInput === 'bomb'){
    return 'bomb'
  }
  else {console.log('you type wrong!');}
}

const getComputerChoice = () => {
  const randomNumber = Math.floor((Math.random() * 3));
  switch(randomNumber){
    case 0: return 'paper';
    break;
    case 1: return 'scissors';
    break;
    case 2: return 'rock';
  }
}


const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === 'bomb') {
    return 'You win';
  }
  if (userChoice === computerChoice) {
    return 'tie';
  }
  if (userChoice === 'rock'){
    if (computerChoice === 'paper'){
      return 'Computer wins'
    }
    else {
      return 'You win'
    }
  }
  if (userChoice === 'paper'){
    if (computerChoice === 'scissors'){
      return 'Computer wins'
    }
    else {
      return 'You win'
    }
  }
  if (userChoice === 'scissors'){
    if (computerChoice === 'rock'){
      return 'Computer wins'
    }
    else {
      return 'You win'
    }
  }
}

const playGame = (userChoice, computerChoice) =>{
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log('you threw '+userChoice);
  console.log('computer threw '+computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}

playGame();

Hi @sandy7052201264! And Welcome to the community!

The problem you are having is actually in the parameters of your playGame function. As you can see when you call the function you are not sending any parameter, but in the function declaration you have the userChoice and the computerChoice decleared.

So as you have already declared them as parameters it sends an error because you want to declare them once again but as constants. To fix this get rid of the parameters and just declare your function as:

const playGame = () =>{
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log('you threw '+userChoice);
  console.log('computer threw '+computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}

This should work fine.

If you have problems later on, don’t forget to post it in the community so we can help you. If you found the post helpful and it answered your question don’t forget to assign this post as the solution.

Happy coding!

Thank you so much! :grinning:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.