Here’s my code for the Rock, Paper, or Scissors project:
Hey everyone!
After hours of checking for errors 1000 times, which ended up being misplaced semicolons, curly brackets and typos
, I wanted to share my code for other desperate souls out there:
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock') {
return userInput;
} else if (userInput === 'paper') {
return userInput;
} else if (userInput === 'scissors') {
return userInput;
} else if (userInput === 'bomb') {
return userInput;
} else {
console.log('Input not valid!');
}
};
const getComputerChoice = () => {
const 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 is a tie!';
}
if (userChoice === 'bomb') {
if (computerChoice === 'rock' || computerChoice === 'paper' || computerChoice === 'scissors') {
return 'You\'re the BOMB!';
}
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer has won!';
} else {
return 'Congratulations, you won!';
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return "Sorry, computer won!";
} else {
return "Congratulations, you won!";
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'Sorry, computer wins!';
} else {
return 'Congratulations, you won!';
}
}
};
const playGame = () => {
const userChoice = getUserChoice('bomb');
const computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
};
playGame();
Thank you very much for all the support on this forum and walkthrough videos!
Best regards,
Cristina
2 Likes
Hey Cristina
I see that the user choice is hard coded to bomb
. Why don’t you set it randomly instead, this way every time the code run the result is going to be unexpected. 
Hey,
sorry but I don’t know exactly what you mean.
Here “bomb” was supposed to be like a “cheat code” to beat the computer. Making it random would defeat the purpose no?
I’m a newbie so maybe I didn’t fully understand :)).
Thanks for your reply.
Hey,
well I think it would be nice to see every time a different result. The purpose of the cheat is not defeated anyhow, because it’s available only to the player, with a random choice the player still has about 25% (1 out of 4 possible choices) to certainly win, whatever is the choice of the computer.
Does this make sense? 
Hmm, you mean like this?
const getUserChoice = () => {
const randomChoice = Math.floor(Math.random() * 4);
switch (randomChoice) {
case 0:
return 'rock'
break;
case 1:
return 'paper'
break;
case 2:
return 'scissors'
break;
case 3:
return 'bomb'
break;
}
}
const getComputerChoice = () => {
const 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 is a tie!';
}
if (userChoice === 'bomb') {
if (computerChoice === 'rock' || computerChoice === 'paper' || computerChoice === 'scissors') {
return 'You\'re the BOMB!';
}
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer has won!';
} else {
return 'Congratulations, you won!';
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return "Sorry, computer won!";
} else {
return "Congratulations, you won!";
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'Sorry, computer wins!';
} else {
return 'Congratulations, you won!';
}
}
};
const playGame = () => {
const userChoice = getUserChoice();
const computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
};
playGame();
So, instead of having a user input, you have a random choice, as with the computer?
Hope I got it right :))
Yeah I meant that. Cool, I won 2 out of 5, 2 ties and 1 lose, not bad
Have you played it?
