Hi everyone.
I’m new to coding.
I’m in middle of doing the Rock, paper, scissors project in Javascript.
I can’t seem to understand why my code is printing the following:
You threw rock
The computer threw () => {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 2:
return ‘scissors’
}
}
Congratulations, you won!
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else {
console.log('Please input either rock, paper or scissors')
}
}
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 'The game is a tie!';
}
if (userChoice === 'rock') {
if(computerChoice === 'paper')
return 'Computer has won!'
} else {
return 'Congratulations, you won!'
}
if (userChoice === 'paper') {
if(computerChoice === 'scissors')
return 'Computer, you won!'
} else {
return 'Congratulations, you won!'
}
if (userChoice === 'scissors') {
if(computerChoice === 'rock')
return 'Computer has won!'
} else {
return 'Congratulations, you won!'
}
}
const playGame = () => {
const userChoice = getUserChoice('rock')
const computerChoice = getComputerChoice
console.log(`You threw ${userChoice}`)
console.log(`The computer threw ${computerChoice}`)
console.log(determineWinner(userChoice, computerChoice))
};
playGame()
Why is it printing the entire switch statement and above it?
Any help and guidance would be much appreciated.
Thanks.