-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (63 loc) · 2.27 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
let userScore = 0;
let computerScore = 0;
const userScore_span = document.getElementById('user-score');
const computerScore_span = document.getElementById('computer-score');
const scoreBoard_div = document.querySelector('.score-board');
const result_p = document.querySelector('.result > p');
const rock_div = document.getElementById('rock');
const paper_div = document.getElementById('paper');
const scissors_div = document.getElementById('scissors');
function getComputerChoice() {
const choices = ['rock', 'paper', 'scissors'];
const randomNumber = Math.floor(Math.random() * 3);
return choices[randomNumber];
}
function convertToWord(letter) {
if (letter === 'rock') return "Rock";
if (letter === 'paper') return "Paper";
return "Scissor";
}
function win(user, computer) {
userScore++;
userScore_span.innerHTML = userScore;
result_p.innerHTML = `<p>${convertToWord(user)} beats ${convertToWord(computer)}. You win!!🏆🏆</p>`;
}
function loses(user, computer) {
computerScore++;
computerScore_span.innerHTML = computerScore;
result_p.innerHTML = `<p>${convertToWord(computer)} beats ${convertToWord(user)}. You lose...😢😢</p>`;
}
function draw(user, computer) {
const userName = ' (user)'.fontsize(3).sup();
const compName = ' (comp)'.fontsize(3).sup();
result_p.innerHTML = `<p>It was a draw!😐😐 You both chose ${convertToWord(user)}</p>`;
}
function game(userChoice) {
const computerChoice = getComputerChoice();
switch (userChoice + computerChoice) {
case 'paperrock':
case 'rockscissors':
case 'scissorspaper':
win(userChoice, computerChoice);
// console.log("user wins");
break;
case 'rockpaper':
case 'scissorsrock':
case 'paperscissors':
loses(userChoice, computerChoice);
// console.log("computer wins");
break;
case 'rockrock':
case 'scissorsscissors':
case 'paperpaper':
draw(userChoice, computerChoice);
// console.log("draw");
break;
}
}
function main() {
rock_div.addEventListener('click', () => game('rock'));
paper_div.addEventListener('click', () => game('paper'));
scissors_div.addEventListener('click', () => game('scissors'));
}
main();