-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrockPaperScissors.js
57 lines (50 loc) · 1.36 KB
/
rockPaperScissors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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();