-
Notifications
You must be signed in to change notification settings - Fork 0
/
JS-exercise-19
44 lines (36 loc) · 1.31 KB
/
JS-exercise-19
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
let allAdvice = ['Walcz dalej', 'Poczekaj chwilę'];
const btn = document.querySelector('.add');
const input = document.querySelector('input');
const reset = document.querySelector('.clean');
const showMe = document.querySelector('.showAdvice');
const h1 = document.querySelector('h1')
const showOptions = document.querySelector('.showOptions')
const showAdvice = (e) => {
e.preventDefault(); //to jest koniecznie inaczej po odświeżeniu strony wynik się resetuje
const newAdvice = input.value;
if (newAdvice.length) {
allAdvice.push(newAdvice); //uwaga - najpierw podajemy nazwę tablicy, dopiero później argument
alert(`Dodano ${newAdvice}`)
input.value = "";
console.log(allAdvice)
}
}
const resetAll = (e) => {
e.preventDefault();
allAdvice = [];
//inaczej można to zrobić allAdvice.length = 0;
}
const toDo = (e) => {
e.preventDefault();
const index = Math.floor(Math.random()*allAdvice.length);
let needToDo = allAdvice[index];
h1.textContent = needToDo;
//można inaczej h1.textContent = allAdvice[index];
}
const options = () => {
alert(allAdvice.join(', '));
}
btn.addEventListener('click', showAdvice);
reset.addEventListener('click', resetAll);
showMe.addEventListener('click', toDo);
showOptions.addEventListener('click', options);