Although i’m getting no syntax errors, my code will not execute properly. When I have the value of ‘rock’ attached to userChoice variable, and the computerChoice variable randomly chooses ‘scissors’, it still prints “Its a tie!”. // output: You threw rock and the computer threw scissors. Its a tie!
I’ve looked over others code for this same project, and I was unable to find any discrepancies that would alter the way the code was logged to the console.
It would be great if someone could look over what I have so far and give me some pointers on how to get it to log the proper string to the console from the conditional statement, or in general on how to continue to improve my code for the future.
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if(userInput === “rock” || userInput === “paper” || userInput === “scissors”) {
return userInput;
} else {
console.log("ERROR: " + userInput)
}
};
const getComputerChoice = () => {
let randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return "rock";
case 1:
return "scissors";
case 2:
return "paper";
}
};
const determineWinner = (userChoice, computerChoice) => {
userChoice = getUserChoice(userChoice);
computerChoice = getComputerChoice(computerChoice);
if(userChoice = computerChoice) {
return "Its a tie!";
}
if(userChoice === “rock”) {
if(computerChoice === "paper") {
return "Computer Won!";
} else {
return "You Won!";
}
}
if(userChoice === “paper”) {
if(computerChoice === "scissors") {
return "Computer Won!";
} else {
return "You Won!";
}
}
if(userChoice === “scissors”) {
if(computerChoice === "rock") {
return "Computer Won!";
} else {
return "You Won!";
}
}
};
const playGame = () => {
const userChoice = getUserChoice(‘rock’);
const computerChoice = getComputerChoice();
console.log(You threw ${userChoice} and the computer threw ${computerChoice}.
);
console.log(determineWinner(userChoice, computerChoice));
}
playGame();