-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
43 lines (38 loc) · 1.14 KB
/
script.js
File metadata and controls
43 lines (38 loc) · 1.14 KB
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
const quoteText = document.getElementById('quote');
const quoteAuthor = document.getElementById('author');
const newBtn = document.getElementById('new-quote');
const tweetBtn = document.getElementById('tweet');
const APIURL = 'https://type.fit/api/quotes';
let quotes = [];
//function to choose a new quote
function newQuote() {
//choose a random quote
const quote = quotes[Math.floor(Math.random() * quotes.length)];
if(!quote.author) {
quoteAuthor.textContent = "UNKNOWN";
}
else {
quoteAuthor.textContent = quote.author;
}
quoteText.textContent = quote.text;
}
//function to tweet Quote
function tweetQuote() {
const tweetUrl = `https://twitter.com/intent/tweet?text=${quoteText.textContent} - ${quoteAuthor.textContent}`;
window.open(tweetUrl, '_blank');
}
//function to fetch quotes
async function getQuotes() {
try {
const response = await fetch(APIURL);
quotes = await response.json();
}
catch(error) {
alert(error.message);
}
}
//on load
getQuotes();
//add event listeners
newBtn.addEventListener('click', newQuote);
tweetBtn.addEventListener('click', tweetQuote);