-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
63 lines (53 loc) · 1.82 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
let randomNumber = Math.floor(Math.random() * 100);
console.log(randomNumber);
const input = document.querySelector("#input");
const answer = document.querySelector("#answer");
const button = document.querySelector("#guess-button");
// let guessedNumber = 0;
// const updateValue = (e) => {
// guessedNumber = e.target.value;
// };
button.hasAttribute("disabled") ? (button.style.cursor = "not-allowed") : null;
input.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
e.preventDefault();
button.click();
}
});
const toggleDisabled = () => {
button.toggleAttribute("disabled");
button.classList.toggle("disabled");
};
!input.value ? toggleDisabled() : null;
input.addEventListener("input", (e) => {
if (!input.value) {
button.hasAttribute("disabled") ? null : toggleDisabled();
} else {
button.hasAttribute("disabled") ? toggleDisabled() : null;
}
});
const playGuess = () => {
if (button.innerHTML === "Rejouer ?") {
input.value = "";
button.innerHTML = "Guess";
button.style.backgroundColor = "#e01c1c";
answer.innerHTML = "";
randomNumber = Math.floor(Math.random() * 100);
console.log(randomNumber);
} else {
let guess = parseInt(input.value);
// guess = guessedNumber;
if (guess == randomNumber) {
answer.innerHTML = `Bravo ! Il fallait bien deviner ${guess}`;
button.style.backgroundColor = "#1ee65d";
button.innerHTML = "Rejouer ?";
} else if (guess > 100 || parseInt(guess) < 0 || !Number.isInteger(guess)) {
answer.innerHTML = `${guess} n'est pas une entrée valide`;
} else if (guess < randomNumber) {
answer.innerHTML = `Faux ! C'est plus grand que ${guess}`;
} else if (guess > randomNumber) {
answer.innerHTML = `Faux ! C'est plus petit que ${guess}`;
}
}
};
button.addEventListener("click", playGuess);