-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
155 lines (134 loc) · 3.59 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const startGameBtn = document.getElementById('start-game-btn');
const ROCK = 'ROCK';
const PAPER = 'PAPER';
const SCISSORS = 'SCISSORS';
const DEFAULT_USER_CHOICE = ROCK;
const RESULT_DRAW = 'DRAW';
const RESULT_PLAYER_WINS = 'PLAYER_WINS';
const RESULT_COMPUTER_WINS ='COMPUTER_WINS';
let gameIsRunning = false;
const getPlayerChoice = ()=>{
const selection = prompt(`${ROCK} , ${PAPER} Or ${SCISSORS}?`,'').toUpperCase();
if (
selection !== 'ROCK' &&
selection !== 'PAPER' &&
selection !== 'SCISSORS'
) {
alert('Invalid choice! we chose ${DEFAULT_USER_CHOICE} ROCK for you!');
return DEFAULT_USER_CHOICE;
}
return selection;
};
const getComputerChoice = ()=>{
const randomValue = Math.random();
if(randomValue < 0.34){
return ROCK;
} else if (randomValue < 0.67){
return PAPER;
} else {
return SCISSORS;
}
};
const getWinner = (
cChoice,
pChoice = DEFAULT_USER_CHOICE
) =>
cChoice === pChoice
? RESULT_DRAW
: (cChoice ===ROCK && pChoice=== PAPER)||
(cChoice===SCISSORS && pChoice === SCISSORS ||
cChoice === SCISSORS && pChoice === ROCK)
? RESULT_PLAYER_WINS
: RESULT_COMPUTER_WINS;
/*
if (cChoice === pChoice){
return RESULT_DRAW;
}else if(
cChoice === ROCK && pChoice===PAPER ||
cChoice===SCISSORS && pChoice === SCISSORS ||
cChoice === SCISSORS && pChoice === ROCK)
{
return RESULT_PLAYER_WINS;
}else {
return RESULT_COMPUTER_WINS;
} */
startGameBtn.addEventListener('click', ()=> {
if (gameIsRunning) {
return;
}
gameIsRunning = true;
console.log('Game is starting..');
const playerChoice = getPlayerChoice();
//console.log(playerSelection);
const computerChoice = getComputerChoice();
let winner;
if(playerChoice){
winner = getWinner (computerChoice,playerChoice);
}else {
winner = getWinner(computerChoice);
}
//const winner = getWinner(computerChoice, playerChoice);
let message = `You picked ${playerChoice || DEFAULT_USER_CHOICE}, computer picked $(computerChoice),therefor you`;
if(winner===RESULT_DRAW){
message = message + 'had a draw';
} else if (winner === RESULT_PLAYER_WINS){
message = message + 'won';
}else {
message = message + 'lost';
}
alert (message);
gameIsRunning = false;
});
// not releted to game
const combine = (resultHnadler,operation, ...numbers ) => {
const validateNumber = (number) => {
return isNaN(number) ? 0 : number;
};
let sum = 0;
for (const num of numbers){
if(operation === 'ADD') {
sum += validateNumber(num);
} else {
sum -= validateNumber(num);
}
}
resultHnadler(sum);
};
/*
const subtractUp = function(resultHnadler, ...numbers){
let sum = 0;
for (const num of numbers){ //dont use that
sum -= num;
}
resultHnadler(sum, 'The result after adding all number is');
}; */
const showResult = (messageText, result ) => {
alert(messageText + ' ' +result);
};
combine(
showResult.bind(this, 'The result after adding all numbers is: '),
'ADD',
1,
5,
'fdsa',
-3,
6,
10);
combine(
showResult.bind(this, 'The result after adding all numbers is: '),
'ADD',
1,
5,
-10,
-3,
6,
10,
25,
88);
combine(
showResult.bind(this, 'The result after adding all numbers is: '),
'SUBTRACT',
1,
10,
15,
20);