Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. {
  2.  
  3.  
  4. const fetchJSON = () => {
  5. fetch('../src/assets/data/quotes.json')
  6. .then(response => {
  7. return response.json();
  8. })
  9. .then(data => {
  10. const quotes = data.quotes;
  11. const randomQuote = quotes[Math.floor(Math.random() * quotes.length)];
  12. console.log(randomQuote);
  13. loadQuotes(quotes);
  14. loadRandomQuote(randomQuote);
  15. console.log(quotes);
  16. })
  17. .catch(err => {
  18. console.log(err);
  19. });
  20. };
  21.  
  22. const loadRandomQuote = randomQuote => {
  23. const $h1 = document.querySelector('.title__quote');
  24. $h1.textContent = randomQuote.quote;
  25. };
  26.  
  27. const loadQuotes = quotes => {
  28. const $quotesList = document.querySelector('.list');
  29.  
  30. quotes.forEach(quote => {
  31. const $li = document.createElement(`li`);
  32. $li.classList.add(`list__quote`);
  33. $li.innerHTML = `<a class="">
  34. <span class="quote">${quote.quote}</span>
  35. <span class="author">${quote.author}</span>
  36. </a>
  37. `;
  38. $quotesList.appendChild($li);
  39. });
  40. };
  41.  
  42. const init = () => {
  43. fetchJSON();
  44. };
  45. init();
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement