-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.js
49 lines (41 loc) · 1.21 KB
/
random.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
// <⚠️ DONT DELETE THIS ⚠️>
//import "./styles.css";
// <⚠️ /DONT DELETE THIS ⚠️>
const rangeBar = document.getElementById("jsRange");
const rangeEx = document.querySelector(".jsEx");
const userAns = document.querySelector(".jsInput");
const play = document.querySelector(".jsPlay");
const win = document.querySelector(".jsWin");
const lose = document.querySelector(".jsLose");
const result = document.querySelector(".jsResult");
let KEY=0;
let USERANS =0;
function handleRangeChange(event){
const max = event.target.value;
rangeEx.innerText = `Generate a number between 0 and ${max}`;
}
function defineKey(max){
KEY = Math.floor(Math.random()*max);
}
function handleSubmit(event){
event.preventDefault();
USERANS = event.target.value;
}
function handleClick(){
defineKey(rangeBar.value);
result.innerText = `You chose: ${USERANS}, the machine chose: ${KEY}.`
if (KEY == USERANS){
lose.classList.add("lost");
win.classList.remove("won");
}
else {
win.classList.add("won");
lose.classList.remove("lost");
}
}
function init(){
rangeBar.addEventListener("input", handleRangeChange);
userAns.addEventListener("input", handleSubmit);
play.addEventListener("click", handleClick);
}
init();