Skip to content

Commit

Permalink
Create rockPaperScissors.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jiseonk committed Sep 18, 2020
1 parent 7bfbd8f commit c65f8be
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions rockPaperScissors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if(userInput === 'rock'||'paper'||'scissors'){
return userInput;
} else {
console.log('error!');
}
};


const getComputerChoice = () => {
let num = Math.random()*3;
num = Math.floor(num);

switch(num) {
case 0 : return 'rock'; break;
case 1 : return 'paper'; break;
case 2 : return 'scissors'; break;
default : return 'error!'; break;
}
};

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 '🎊Congratulations! You won!🎊';
}
}
if(userChoice ==='paper'){
if(computerChoice==='scissors'){
return 'the computer won!';
} else {
return '🎊Congratulations! You won!🎊';
}
}
if(userChoice ==='scissors'){
if(computerChoice==='rock'){
return 'the computer won!';
} else {
return '🎊Congratulations! You won!🎊';
}
}
};


const playGame = (userChoice = getUserChoice('rock'),computerChoice = getComputerChoice()) => {
console.log(`user = ${userChoice}`);
console.log(`computer = ${computerChoice}`);
console.log(determineWinner(userChoice,computerChoice));
};

playGame();

0 comments on commit c65f8be

Please sign in to comment.