-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
24 lines (22 loc) · 915 Bytes
/
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
const adviceNumber = document.getElementById("advice-number");
const adviceText = document.getElementById("advice-text");
const dice = document.getElementById("dice");
dice.addEventListener("click", function () {
const adviceData = async function () {
try {
// get advice data from api
let num = Math.floor(Math.random() * 224);
const res = await fetch(`https://api.adviceslip.com/advice/${num}`);
const data = await res.json();
// change UI upon clicking the dice
adviceText.style.color = `#cee3e9`;
adviceNumber.innerHTML = `Advice #${data.slip.id}`;
adviceText.innerHTML = `${data.slip.advice}`;
} catch (err) {
// display error in red clr
adviceText.style.color = "crimson";
adviceText.innerHTML = `An error occured, please wait a second and generate another advice or reload the page 😓😓${err}`;
}
};
adviceData();
});