-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
97 lines (83 loc) · 3.31 KB
/
script.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
// Getting all Elements from the DOM
const computerChoice = document.querySelector('#computer-choice');
const playerChoice = document.querySelector('#player-choice');
const result = document.querySelector('#result');
const rockBtn = document.querySelector('#rock-btn');
const paperBtn = document.querySelector('#paper-btn');
const scissorsBtn = document.querySelector('#scissors-btn');
const startBtn = document.querySelector('#start-btn');
const playAgainBtn = document.querySelector('#play-again');
const gameBtnDiv = document.querySelector('#game-btns');
const choices = ['🪨', '🧻', '✂️']
// Function to get a randomIndex to use on the choices Array
function getRandomIndex() {
let Index = Math.floor(Math.random() * choices.length);
return Index;
}
// Function to render Computer Choices and Results
function game() {
computerChoice.innerHTML = choices[getRandomIndex()];
if (playerChoice.innerHTML === "🪨" && computerChoice.innerHTML === "🪨") {
result.innerHTML = "🫶";
} else if (playerChoice.innerHTML === "🪨" && computerChoice.innerHTML === "🧻") {
result.innerHTML = "💔";
} else if (playerChoice.innerHTML === "🪨" && computerChoice.innerHTML === "✂️") {
result.innerHTML = "❤️";
} else if (playerChoice.innerHTML === "🧻" && computerChoice.innerHTML === "🪨") {
result.innerHTML = "❤️";
} else if (playerChoice.innerHTML === "🧻" && computerChoice.innerHTML === "🧻") {
result.innerHTML = "🫶";
} else if (playerChoice.innerHTML === "🧻" && computerChoice.innerHTML === "✂️") {
result.innerHTML = "💔";
} else if (playerChoice.innerHTML === "✂️" && computerChoice.innerHTML === "🪨") {
result.innerHTML = "💔";
} else if (playerChoice.innerHTML === "✂️" && computerChoice.innerHTML === "🧻") {
result.innerHTML = "❤️";
} else if (playerChoice.innerHTML === "✂️" && computerChoice.innerHTML === "✂️") {
result.innerHTML = "🫶";
}
}
// Hides all 3 game buttons
function hideGameBtns() {
gameBtnDiv.classList.remove('show');
gameBtnDiv.classList.add('hide');
playAgainBtn.classList.remove('hide');
playAgainBtn.classList.add('show');
}
// resets the game and shows the 3 game buttons
function playAgain() {
computerChoice.innerHTML = '';
playerChoice.innerHTML = '';
result.innerHTML = '';
playAgainBtn.classList.remove('show');
playAgainBtn.classList.add('hide');
gameBtnDiv.classList.remove('hide');
gameBtnDiv.classList.add('show');
}
// Rockbtn Event added
rockBtn.addEventListener('click', function() {
playerChoice.innerHTML = "🪨";
game();
hideGameBtns();
})
// Paperbtn Event added
paperBtn.addEventListener('click', function() {
playerChoice.innerHTML = '🧻';
game();
hideGameBtns();
})
// Scissorsbtn Event added
scissorsBtn.addEventListener('click', function() {
playerChoice.innerHTML = "✂️";
game();
hideGameBtns();
})
// Playagain btn event added
playAgainBtn.addEventListener('click', playAgain)
// Starts the game and renders the right buttons
startBtn.addEventListener('click', function() {
startBtn.classList.remove('show');
startBtn.classList.add('hide');
gameBtnDiv.classList.remove('hide');
gameBtnDiv.classList.add('show');
})