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