-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
72 lines (62 loc) · 2.12 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
const form = document.getElementById('form');
const input = document.getElementById("input");
const submit = document.getElementById("submit");
const start = document.getElementById("start");
const result = document.querySelector(".result");
const hint = document.querySelector(".hint");
// form.addEventListener('submit',(e) => {
// e.preventDefault();
// console.log("Submitted");
// })
(function a() {
let random;
function randomNumber() {
random = Math.floor(Math.random() * 100) + 1;
}
randomNumber();
let count = 0;
let arr = [];
let str = '';
submit.addEventListener("click", () => {
let inputVal = parseInt(input.value);
if (count === 7) {
result.textContent = "You lose";
alert("Game Over" + " number was " + random);
return;
}
if (inputVal) {
if (inputVal == random) {
result.textContent = "You won!";
arr.push(inputVal);
hint.innerHTML = "Your Gusess : " + str + " " + inputVal.toString() + " and you took " + ++count + " guesses";
start.disabled = false;;
submit.disabled = true;
return;
}
else if (random - inputVal < 0) {
count++;
arr.push(inputVal);
// str = str + " " + inputVal.toString();
result.innerHTML = "Too High!"
hint.innerHTML = "Your Gusess : " + arr.join(', ');
}
else {
count++;
arr.push(inputVal);
// str = str + " " + inputVal.toString();
result.innerHTML = "Too Low!"
hint.innerHTML = "Your Gusess : " + arr.join(', ');
}
input.value = '';
}
});
start.addEventListener("click", () => {
result.textContent = "";
hint.innerHTML = "";
count = 0;
str = '';
randomNumber();
start.disabled = true;
submit.disabled = false;
});
})();