Skip to content

Instantly share code, notes, and snippets.

@towerofnix
Created November 20, 2016 20:54
Show Gist options
  • Save towerofnix/470a32f40c51799a91dfc66b5b22dd6a to your computer and use it in GitHub Desktop.
Save towerofnix/470a32f40c51799a91dfc66b5b22dd6a to your computer and use it in GitHub Desktop.
Picks a country for you to study. (Actually, three.)
fetch('https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_and_their_capitals_in_native_languages')
.then(res => res.text())
.then(resText => (new DOMParser()).parseFromString(resText, 'text/html'))
.then(doc => (
Array.from(doc.getElementsByClassName('wikitable'))
.map(tbl => tbl.querySelectorAll('tr'))
.reduce((f, tf) => f.concat(Array.from(tf)), [])
.map(r => r.children[0])
.map(t => t.innerText)
))
.then(countries => {
const picked = []
for (i = 0; i < 3; i++) {
picked.push(countries[Math.floor(Math.random() * countries.length)])
}
return picked
})
.then(picked => {
console.log(picked)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment