Created
March 9, 2022 01:50
-
-
Save stephenmathieson/96bee738447d0688d2b99ed53e5c4663 to your computer and use it in GitHub Desktop.
Is a well-known person alive?
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
const { JSDOM } = require('jsdom'); | |
const got = require('got').default; | |
const WIKIPEDIA_URL = 'https://en.wikipedia.org/wiki'; | |
/** | |
* Check if the person with `firstName` and `lastName` is currently alive. | |
* | |
* @param {string} firstName | |
* @param {string} lastName | |
*/ | |
const isPersonAlive = async (firstName, lastName) => { | |
const res = await got(`${WIKIPEDIA_URL}/${firstName}_${lastName}`); | |
const dom = new JSDOM(res.body); | |
const { document } = dom.window; | |
const ths = document.querySelectorAll('table.vcard th[scope=row]'); | |
for (const th of ths) { | |
const text = th.innerHTML.trim(); | |
if (text === 'Died') { | |
return false; | |
} | |
} | |
return true; | |
}; | |
(async () => { | |
const [hawking, putin] = await Promise.all([ | |
isPersonAlive('Stephen', 'Hawking'), | |
isPersonAlive('Vladimir', 'Putin'), | |
]); | |
console.log('Stephen Hawking is', hawking ? 'alive' : 'dead'); | |
console.log('Vladimir Putin is', putin ? 'alive' : 'dead'); | |
})(); |
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
{ | |
"name": "is-person-alive", | |
"version": "1.0.0", | |
"main": "is-person-alive.js", | |
"license": "MIT", | |
"dependencies": { | |
"got": "11", | |
"jsdom": "19" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment