const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput
} else {
console.log('Error!')
}
}
const getcomputerChoice = () => {
const 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 'Tie'
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'computer won'
} else {
return 'user won'
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'computer won'
} else {
return 'user won'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'computer won'
} else {
return 'user won'
}
}
}
const playGame = () => {
var userChoice = getUserChoice()
console.log(userChoice)
var computerChoice = getcomputerChoice()
console.log(computerChoice)
console.log(determineWinner(userChoice, computerChoice))
}
console.log(determineWinner())
So I wanted to do a project with JS, was following codeacadem's course on the subject. But for some reason I am always getting a tie which I do not understand why. Can anyone please help me? I thought this is the correct code and every single object was working properly.
UPDATE:
After giving a parameter to the userchoice (getUserChoice('scissors')) I got this error: userInput = userInput.toLowerCase();
TypeError: Cannot read properties of undefined (reading 'toLowerCase')
Initializing playgame didnt solve the issue