-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
100 lines (67 loc) · 2.69 KB
/
main.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
const rock = document.querySelector('.rock');
const paper = document.querySelector('.paper');
const scissors = document.querySelector('.scissors');
const imgComp = document.querySelector('.computer');
const imgUser = document.querySelector('.player');
const h1 = document.querySelector('h1');
const input = document.querySelector('input');
const options = [rock, paper, scissors];
let userScore = 0, compScore = 0;
let compChoice;
h1.textContent = `${userScore} - ${compScore}`;
options.forEach(chooseOption);
function chooseOption(item) {
item.addEventListener('click', showResult)
};
function showResult() {
//USER CHOICE
let userChoice = this.className;
imgUser.style.backgroundImage = `url('images/${userChoice}.jpg')`;
// console.log(`url('images/${userChoice}.jpg');`);
if (input.checked == false) {
//COMP CHOICE
compChoice = options[Math.floor(Math.random() * 3)].className;
// console.log(compChoice);
imgComp.style.backgroundImage = `url('images/${compChoice}.jpg')`;
checkScore(userChoice, compChoice);
} else {
winning(userChoice);
}
};
function checkScore(choice, comp) {
console.log('uncheked');
switch (choice) {
case 'rock':
(comp === 'scissors') ? userScore++ : (comp === 'paper') ? compScore++ : console.log();
// ((comp == 'scissors') ? userScore++ : ((comp == 'rock') ? userScore++ : ((comp == 'paper') ? userScore++)));
break;
case 'paper':
(comp === 'scissors') ? compScore++ : (comp === 'rock') ? userScore++ : console.log();
break;
case 'scissors':
(comp === 'rock') ? compScore++ : (comp === 'paper') ? userScore++ : console.log();
break;
};
// console.log(choice, comp, userScore + ' - ' + compScore);
h1.textContent = `${compScore} - ${userScore}`;
};
function winning(choice) {
console.log('cheked');
switch (choice) {
case 'rock':
userScore++;
imgComp.style.backgroundImage = `url('images/scissors.jpg`;
// ((comp == 'scissors') ? userScore++ : ((comp == 'rock') ? userScore++ : ((comp == 'paper') ? userScore++)));
break;
case 'paper':
userScore++;
imgComp.style.backgroundImage = `url('images/rock.jpg`;
break;
case 'scissors':
userScore++;
imgComp.style.backgroundImage = `url('images/paper.jpg`;
break;
};
// console.log(choice, comp, userScore + ' - ' + compScore);
h1.textContent = `${compScore} - ${userScore}`;
};