Skip to content

Commit b42333b

Browse files
committed
change: html and script
1 parent 1e4fdd2 commit b42333b

File tree

2 files changed

+71
-18
lines changed

2 files changed

+71
-18
lines changed

09_quote-generator/index.html

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,32 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>Quote Generator</title>
77
<script src="https://cdn.tailwindcss.com"></script>
8+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
89
</head>
9-
<body>
10-
<div id="main">
11-
<div id="quote">
12-
<h1 id="statement"></h1>
13-
<span id="author"></span>
10+
<body class="flex items-center justify-center min-h-screen font-sans">
11+
12+
<div id="main" class="bg-white p-8 rounded-xl shadow-2xl max-w-lg w-full mx-4 text-center">
13+
14+
<div id="quote" class="mb-8">
15+
<i class="fas fa-quote-left text-4xl text-gray-300 mb-4 block text-left"></i>
16+
<h1 id="quote-text" class="text-3xl font-bold text-gray-800 mb-4 leading-tight">
17+
Loading...
18+
</h1>
19+
<span id="author" class="text-xl text-gray-500 italic block mt-4"></span>
1420
</div>
15-
<hr>
16-
<div id="buttons">
17-
<a href="www.x.com" id="tweet">Tweet</a>
18-
<button id="next-quote">Next Quote</button>
21+
22+
<hr class="border-gray-200 my-6">
23+
24+
<div id="buttons" class="flex justify-between items-center">
25+
<a href="#" id="tweet" target="_blank" class="bg-blue-500 hover:bg-blue-600 transition-colors text-white font-bold py-2 px-6 rounded shadow-md flex items-center gap-2">
26+
<i class="fab fa-twitter"></i> Tweet
27+
</a>
28+
<button id="next-quote" class="bg-gray-800 hover:bg-gray-900 transition-transform active:scale-95 text-white font-bold py-2 px-6 rounded shadow-md">
29+
Next Quote
30+
</button>
1931
</div>
20-
<p>This random quote generated by Niraj</p>
32+
33+
<p class="text-gray-400 text-xs mt-8">This random quote generated by Niraj</p>
2134
</div>
2235

2336
<script src="script.js"></script>

09_quote-generator/script.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
1-
const statement = document.getElementById('statement');
2-
const authorName = document.getElementById('author');
3-
const actionButton = document.getElementById('buttons');
4-
const nextQuote = document.getElementById('next-quote');
1+
const quoteText = document.getElementById('quote-text');
2+
const authorText = document.getElementById('author');
3+
const twitterBtn = document.getElementById('tweet');
4+
const nextQuoteBtn = document.getElementById('next-quote');
55

6-
nextQuote.onclick = getNewQuote();
7-
actionButton.addEventListener('')
6+
let apiQuotes = [];
87

9-
function getNewQuote() {
8+
async function getQuotes() {
9+
const url = "https://dummyjson.com/quotes"; // New URL
10+
try {
11+
const response = await fetch(url);
12+
const data = await response.json();
13+
14+
apiQuotes = data.quotes;
15+
16+
newQuote();
17+
} catch (error) {
18+
console.error("Error fetching quotes:", error);
19+
quoteText.textContent = "Failed to load quotes.";
20+
}
21+
}
22+
23+
function newQuote() {
24+
if (apiQuotes.length === 0) {
25+
quoteText.textContent = "No quotes available.";
26+
return;
27+
}
28+
29+
const index = Math.floor(Math.random() * apiQuotes.length);
30+
const quote = apiQuotes[index];
31+
32+
quoteText.textContent = quote.quote;
1033

11-
}
34+
if (!quote.author) {
35+
authorText.textContent = "- Unknown";
36+
} else {
37+
authorText.textContent = "- " + quote.author;
38+
}
39+
40+
tweetQuote(quote.quote, authorText.textContent);
41+
}
42+
43+
function tweetQuote(quote, author) {
44+
const twitterUrl = `https://twitter.com/intent/tweet?text=${quote} ${author}`;
45+
twitterBtn.setAttribute('href', twitterUrl);
46+
twitterBtn.setAttribute('target', '_blank');
47+
}
48+
49+
nextQuoteBtn.addEventListener('click', newQuote);
50+
51+
getQuotes();

0 commit comments

Comments
 (0)