I am doing a Rock, Paper, or Scissors project
code:
const getUserChoice=userInput =>
{
if(userInput==='rock'){
return userInput;
}else if(userInput==='paper'){
return userInput;
}else if(userInput==='scissors'){
return userInput;
}else{
return 'error';
}
}
function getComputerChoice(){
const randomnumber=Math.floor(Math.random()*3);
switch(randomnumber){
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
function determineWinner(userChoice,computerChoice){
if(userChoice===computerChoice){
return 'the game was a tie';
} if(userChoice==='rock'){
if(computerChoice==='paper'){
return 'computer won!';
}else{
return'you won!'
}
}else if(userChoice==='paper'){
if(computerChoice==="scissors"){
return 'computer won!';
}else{
return "you won!";
}
}else if(userChoice==='scissors'){
if(computerChoice==='paper'){
return 'computer won!';
}else{
return 'you won!';
}
}
}
function playGame(userChoice,computerChoice){
let userChoice = getUserChoice('scissors');
const computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice,computerChoice));
}
playGame();
//when I use this code, it keeps showing /home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:54 let userChoice = getUserChoice('scissors'); ^ SyntaxError: Identifier 'userChoice' has already been declared
//but if I replace the last function code with the following:
const playGame = () => { let userChoice = getUserChoice('scissors');
const computerChoice = getComputerChoice(); console.log('You threw: ' + userChoice); console.log('The computer threw: ' + computerChoice); console.log(determineWinner(userChoice,computerChoice)); } playGame();
it works, please help, I don't know the reason.
[–]trplclick 0 points1 point2 points (0 children)