-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
38 lines (33 loc) · 1.11 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
const ball = document.querySelector("img");
const input = document.querySelector("input");
const answer = document.querySelector(".answer");
const error = document.querySelector(".error");
const answersArr = [
"Tak!",
"Nie.",
"Może.",
"Ciężko powiedzieć...",
"Nie chcesz znać odpowiedzi na to pytanie... :/",
];
const shakeBall = () => {
ball.classList.add("shake-animation");
setTimeout(checkInput, 1000);
};
const checkInput = () => {
if (input.value !== "" && input.value.slice(-1) === "?") {
generateAnswer();
error.textContent = "";
} else if (input.value !== "" && input.value.slice(-1) !== "?") {
error.textContent = "Pytanie musi być zakończone znakiem '?'.";
answer.textContent = "";
} else {
error.textContent = "Musisz zadać jakieś pytanie!";
answer.textContent = "";
}
ball.classList.remove("shake-animation");
};
const generateAnswer = () => {
const number = Math.floor(Math.random() * 5);
answer.innerHTML = `<span>Odpowiedź:</span> ${answersArr[number]}`;
};
ball.addEventListener("click", shakeBall);