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();