Hello All,
Many thanks in advance for your help. Before i check the video i manage to obtain a similar result but not the same. As well the code could be much simplier and not writing that many lines…but i would like to understand why is showing undefined after the lastest sentence.
Code:
const getUserChoice = userInput =>{
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
return userInput;
} else {console.log("There is an error in your input, please write one of the following options: rock, paper or scissors. Many thanks.")}
};
const getComputerChoice = () =>{
let randomNumber = Math.floor(Math.random() *3);
switch(randomNumber){
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
const determineWinner = (userChoice,computerChoice) =>{
if (userChoice === computerChoice){
return console.log("The game was tie");}
else if (userChoice === 'rock' && computerChoice === 'paper'){
return console.log("Computer has won");}
else if (userChoice === 'rock' && computerChoice === 'scissors'){
return console.log("Human has won");}
else if (userChoice === 'paper' && computerChoice === 'scissors'){
return console.log("Compute has won");}
else if (userChoice === 'paper' && computerChoice === 'rock'){
return console.log("Human has won");}
else if (userChoice === 'scissors' && computerChoice === 'paper'){
return console.log("Human has won");}
else {
return console.log("Computer has 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();
End code
Best regards,
Carlos