Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const getUserChoice = userInput => {
- userInput.toLowerCase()
- if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
- return userInput;
- } else {
- console.log('Input error, choose rock, paper or scissors.');
- }
- };
- const getComputerChoice = () => {
- let randomChoice = Math.floor(Math.random()*3);
- switch(randomChoice){
- 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 was a tie!';
- }
- if(userChoice === 'rock'){
- if(computerChoice === 'paper'){
- return 'The computer won with paper!';
- } else {
- return 'The user won with rock!';
- }
- }
- if(userChoice === 'paper'){
- if(computerChoice === 'scissors'){
- return 'The computer won with scissors!';
- } else {
- return 'The user won with paper!';
- }
- }
- if(userChoice === 'scissors'){
- if(computerChoice === 'rock'){
- return 'The computer won with rock!';
- } else {
- return 'The user won with scissors!';
- }
- }
- }
- const playGame = (userChoice) => {
- userChoice = getUserChoice(userChoice);
- let computerChoice = getComputerChoice();
- console.log(`User is playing with: ${userChoice}\nComputer is playing with: ${computerChoice}`);
- console.log(determineWinner(userChoice, computerChoice));
- }
- playGame('rock');
Advertisement
Add Comment
Please, Sign In to add comment