Here is the following code I have done for a Codecademy course project:
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else {
console.log('Error!');
}
}
const getComputerChoice = () => {
randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
break;
case 1:
return 'paper';
break;
case 2:
return 'scissors';
break;
}
}
const determineWinner = (userChoice, computerChoice) => {
if(userChoice === computerChoice) {
return 'The game was a tie!';
} else if(userChoice === 'rock' && computerChoice === 'paper') {
return 'The computer won the game';
} else {
return 'You have won the game!';
}
if(userChoice === 'paper' && computerChoice === 'scissors') {
return 'The computer won the game';
} else {
return 'You have won the game!';
}
if(userChoice === 'scissors' && computerChoice === 'rock') {
return 'The computer has won the game';
} else {
return 'You have won the game!';
}
}
console.log(determineWinner('paper', 'scissors'));
console.log(determineWinner('paper', 'paper'));
console.log(determineWinner('scissors', 'rock'));
There are strings logging to the console when I run the code, but just not the right ones.
What is it about my if statements in the determineWinner function would make the return not print? For example, when I run this the only block that calls correctly is “‘The game is a tie!’”. The other ones just log “‘You have won the game!’”. I don’t understand what I did wrong here.
Also, please excuse me if I I’m not explaining myself correctly with all of the correct terms regarding JS. I’m still learning, and welcome correction for any terms missused (i.e., call, log, function, etc)!