Skip to content

Instantly share code, notes, and snippets.

@pmartycz
Last active October 6, 2020 17:50
Show Gist options
  • Save pmartycz/8cf0c96b696431343a98a36187a3dc24 to your computer and use it in GitHub Desktop.
Save pmartycz/8cf0c96b696431343a98a36187a3dc24 to your computer and use it in GitHub Desktop.
Quote of the day
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