-
Notifications
You must be signed in to change notification settings - Fork 2
/
personalAdvicer_script.js
104 lines (48 loc) · 1.96 KB
/
personalAdvicer_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
console.log("ok");
let searchBox = document.getElementById('searchBox');
let searchBtn = document.getElementById('searchBtn');
let displayAdvice = document.getElementById('displayAdvice');
searchBox.style.display = 'none';
searchBtn.style.display = 'none';
let optionButton1 = document.getElementById('optionButton1');
let optionButton2 = document.getElementById('optionButton2');
optionButton2.addEventListener('click',()=>{
searchBox.style.display = 'block';
searchBtn.style.display = 'block';
});
searchBtn.addEventListener('click',()=>{
search = searchBox.value;
fetch(`https://api.adviceslip.com/advice/search/${search}`).then(response => response.json()).then((data) => {
if (data.total_results==1) {
displayAdvice.innerHTML = `${data.slip.advice}`
searchBox.value = "";
let x = Math.floor(Math.random()*256)
let y = Math.floor(Math.random()*256)
let z = Math.floor(Math.random()*256)
let bgColor = `rgb(${x},${y},${z})`
displayAdvice.style.background = bgColor;
}
else if(data.total_results!=1){
displayAdvice.innerHTML = `<H1>${data.slips[Math.floor(Math.random()*data.total_results)].advice}</H1>`
searchBox.value = "";
let x = Math.floor(Math.random()*256)
let y = Math.floor(Math.random()*256)
let z = Math.floor(Math.random()*256)
let bgColor = `rgb(${x},${y},${z})`
displayAdvice.style.background = bgColor;
}
}
)
});
optionButton1.addEventListener('click',()=>{
searchBox.style.display = 'none';
searchBtn.style.display = 'none';
fetch("https://api.adviceslip.com/advice").then(response => response.json()).then((data) => {
displayAdvice.innerHTML = `<h1>${data.slip.advice}</h1>`
let x = Math.floor(Math.random()*256)
let y = Math.floor(Math.random()*256)
let z = Math.floor(Math.random()*256)
let bgColor = `rgb(${x},${y},${z})`
displayAdvice.style.background = bgColor;
} )
});