-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
107 lines (78 loc) · 4.42 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
101
102
103
const computerChoices = ["rock", "paper", "scissors"];
const choiceButtons = document.querySelectorAll(".choices button");
const computerText = document.querySelector("h2.bot-choice");
const humanText = document.querySelector("h2.human-choice");
const computerChoice = computerChoices[Math.floor(Math.random() * computerChoices.length)];
const overlay = document.querySelector(".overlay");
const gamedText = document.querySelector(".overlay h3");
const score = document.querySelector(".score h1");
// localStorage.setItem("score", 0);
score.innerText = parseInt(localStorage.getItem("score"))
const timeRange = [1,2,3,4,5]
const computerThinkingText = document.createElement("span");
choiceButtons.forEach(button => {
button.addEventListener("click", e => {
humanText.innerText = `You selected ${e.target.id.toUpperCase()}`;
computerText.appendChild(computerThinkingText);
computerThinkingText.innerText = "Am thinking...🤔";
setTimeout(() => {
computerThinkingText.innerText = `I selected ${computerChoice.toUpperCase()}`;
if(computerChoice.toLocaleLowerCase() == e.target.id.toLocaleLowerCase()){
console.log(computerChoice.toLocaleLowerCase())
console.log(e.target.id.toLocaleLowerCase())
console.log("Tie")
overlay.style.display = "flex";
gamedText.innerText = "Tie!"
localStorage.setItem("score", parseInt(localStorage.getItem("score")) + 1);
}
if(computerChoice.toLocaleLowerCase() == "rock" && e.target.id.toLocaleLowerCase() == "paper"){
console.log(computerChoice.toLocaleLowerCase())
console.log(e.target.id.toLocaleLowerCase())
console.log("You win")
overlay.style.display = "flex";
gamedText.innerText = "You win"
localStorage.setItem("score", parseInt(localStorage.getItem("score")) + 2);
}
if(computerChoice.toLocaleLowerCase() == "rock" && e.target.id.toLocaleLowerCase() == "scissors"){
console.log(computerChoice.toLocaleLowerCase())
console.log(e.target.id.toLocaleLowerCase())
console.log("You lose")
overlay.style.display = "flex";
gamedText.innerText = "You lose"
localStorage.setItem("score", parseInt(localStorage.getItem("score")) - 1);
}
if(computerChoice.toLocaleLowerCase() == "paper" && e.target.id.toLocaleLowerCase() == "rock"){
console.log(computerChoice.toLocaleLowerCase())
console.log(e.target.id.toLocaleLowerCase())
console.log("You lose")
overlay.style.display = "flex";
gamedText.innerText = "You lose"
localStorage.setItem("score", parseInt(localStorage.getItem("score")) - 1);
}
if(computerChoice.toLocaleLowerCase() == "paper" && e.target.id.toLocaleLowerCase() == "scissors"){
console.log(computerChoice.toLocaleLowerCase())
console.log(e.target.id.toLocaleLowerCase())
console.log("You win")
overlay.style.display = "flex";
gamedText.innerText = "You win"
localStorage.setItem("score", parseInt(localStorage.getItem("score")) + 2);
}
if(computerChoice.toLocaleLowerCase() == "scissors" && e.target.id.toLocaleLowerCase() == "rock"){
console.log(computerChoice.toLocaleLowerCase())
console.log(e.target.id.toLocaleLowerCase())
console.log("You win")
overlay.style.display = "flex";
gamedText.innerText = "You win"
localStorage.setItem("score", parseInt(localStorage.getItem("score")) + 2);
}
if(computerChoice.toLocaleLowerCase() == "scissors" && e.target.id.toLocaleLowerCase() == "paper"){
console.log(computerChoice.toLocaleLowerCase())
console.log(e.target.id.toLocaleLowerCase())
console.log("You lose")
overlay.style.display = "flex";
gamedText.innerText = "You lose"
localStorage.setItem("score", parseInt(localStorage.getItem("score")) -1);
}
}, timeRange[Math.floor(Math.random() * timeRange.length)] * 1000);
});
});