Last active
October 6, 2020 17:50
-
-
Save pmartycz/8cf0c96b696431343a98a36187a3dc24 to your computer and use it in GitHub Desktop.
Quote of the day
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function randomQuote() { | |
const quotes = await fetch('http://quotes.cat-v.org/programming/') | |
.then(response => response.text()) | |
.then(html => new DOMParser().parseFromString(html, 'text/html')) | |
.then(dom => [...dom.querySelector('body > article').children]) | |
quotes.shift(); | |
const isSeparator = el => el.tagName === 'HR'; | |
const offsets = quotes.reduce((prev, cur, i) => isSeparator(cur) ? [...prev, i + 1] : prev, [0]); | |
for (let i = offsets[Math.floor(Math.random() * offsets.length)]; | |
i < quotes.length && !isSeparator(quotes[i]); | |
i++) { | |
console.log(quotes[i].textContent); | |
} | |
} | |
randomQuote(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment