Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const getUserChoice = userInput => {
- userInput = userInput.toLowerCase();
- if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
- return userInput;
- } else {
- console.log('Error!');
- }
- };
- function 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 was a tie!'
- }
- if (userChoice === 'rock'){
- if (computerChoice === 'paper'){
- return 'The computer won!'
- } else {
- return 'The player won!'
- }
- }
- if (userChoice === 'paper'){
- if (computerChoice === 'scissors'){
- return 'The computer won!'
- } else {
- return 'The player won!'
- }
- }
- if (userChoice === 'scissors'){
- if (computerChoice === 'rock'){
- return 'The computer won!'
- } else {
- return 'The player won!'
- }
- }
- }
- console.log(determineWinner('paper', 'scissors'));
- console.log(determineWinner('paper', 'paper'));
- console.log(determineWinner('paper', 'rock'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement