-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
69 lines (57 loc) · 2.13 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="icon" type="image/x-icon" href="./img/random.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Quote Generator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="app">
<header>Random Quotes Using API</header>
<section class="quotes">
<div class="quote-text" id="js-quote-text">
Click on the button below to generate a random quote.
</div>
<div class="author-name" id="js-author-name"></div>
</section>
<section class="controls">
<button type="button" id="js-new-quote" class="new-quote button">
Generate a new quote
</button>
</section>
</div>
<script>
const spinner = document.querySelector('#js-spinner');
const newQuoteButton = document.querySelector("#js-new-quote");
newQuoteButton.addEventListener('click', getQuote, false);
async function getQuote(){
try {
const response = await fetch("https://jsonplaceholder.typicode.com/users");
console.log(response);
if(!response.ok){
throw Error(response.statusText);
}
const json = await response.json();
console.log(json);
const index = parseInt(Math.random() * 10);
displayQuote(json[index].text);
displayAuthor(json[index].author);
} catch (error) {
console.log(error);
alert('Failed To fetch API');
}
}
function displayQuote(quote){
const quoteText = document.querySelector("#js-quote-text");
quoteText.innerHTML = `<span>"${quote}"</span>`;
}
function displayAuthor(name){
const authorName = document.querySelector("#js-author-name");
authorName.innerHTML = `<span>- ${name}</span>`;
}
</script>
</body>
</html>