-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (48 loc) · 1.39 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
// ==> VARIABLES
const choices = ["✊", "🤚", "✌️"];
const player1 = document.getElementById("player-1");
const player2 = document.getElementById("player-2");
const resultText = document.getElementById("result");
const playBtn = document.getElementById("play-btn");
// ==> END VARIABLES
// ==> LES FONCTIONS
const generateChoices = () => {
// paper, rock or scissors
let r = Math.floor(Math.random() * 3);
// return the choices of the table regarding the index
return choices[r];
};
// insert in my HTML
// call in my play function line 51
const insertHTML = (choice1, choice2, result) => {
player1.innerHTML = choice1;
player2.innerHTML = choice2;
resultText.innerHTML = result;
};
// who is winner
const decideWinner = (a, b) => {
if (
(a === "✊" && b === "✊") ||
(a === "🤚" && b === "🤚") ||
(a === "✌️" && b === "✌️")
) {
return "Égalité!";
} else if (
(a === "✊" && b === "✌️") ||
(a === "🤚" && b === "✊") ||
(a === "✌️" && b === "🤚")
) {
return "JOUEUR 1 GAGNE!";
} else {
return "JOUEUR 2 GAGNE!";
}
};
const play = () => {
let choice1 = generateChoices();
let choice2 = generateChoices();
let result = decideWinner(choice1, choice2);
insertHTML(choice1, choice2, result);
};
// ==> Event Listeners
// listen "click" on my btn play and start play function
playBtn.addEventListener("click", play);