-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
88 lines (77 loc) · 2.22 KB
/
app.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if(userInput === 'rock' || userInput === 'scissors' ||userInput === 'paper' || userInput === 'bomb') {
return userInput;
}
else
{
console.log('Invalid choice');
}
};
function getComputerChoice() {
let option = Math.floor(Math.random() * 3);
switch(option) {
case 0:
return 'rock';
break;
case 1:
return 'scissors';
break;
case 2:
return 'paper';
break;
}
}
function determineWinner(userChoice , computerChoice) {
//if userchoice is bomb
if(userChoice === 'bomb') {
return 'YOU WON!!!';
}
//checking if it's a tie
if(userChoice === computerChoice) {
return `oh!!! it's a tie`;
}
//If the user choice is rock
if(userChoice === 'rock') {
if(computerChoice === 'paper') {
return `COMPUTER WON!!!!
you are ${userChoice} and computer is ${computerChoice}. `;
}
else{
return `YOU WON!!!!
you are ${userChoice} and computer is ${computerChoice}.`;
}
}
//if the user choice is paper
if(userChoice === 'paper') {
if(computerChoice === 'rock') {
return `YOU WON!!!!
you are ${userChoice} and computer is ${computerChoice}.`;
}
else {
return `COMPUTER WON!!!!
you are ${userChoice} and computer is ${computerChoice}. `;
}
}
//if the user choice is scissors
if(userChoice === 'scissors') {
if(computerChoice === 'rock') {
return `COMPUTER WON!!!
you are ${userChoice} and computer is ${computerChoice}. `;
}
else {
return `YOU WON!!!
you are ${userChoice} and computer is ${computerChoice}.`;
}
}
}
//Play game
function playGame() {
const userChoice = getUserChoice('paper');
const computerChoice = getComputerChoice();
console.log(`Your choice: ${userChoice}`);
console.log(`Computer choice: ${computerChoice}`);
const winner = determineWinner(userChoice,computerChoice);
console.log(winner);
}
playGame();