Last active
November 29, 2018 16:44
-
-
Save microsoftly/e19bb379b9c44f1cfdb0433f1cb71fdc to your computer and use it in GitHub Desktop.
get the count of n hot /r/aww posts that contain cat or dog in the title
This file contains hidden or 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
| import rp = require("request-promise"); | |
| function countTitlesWithCatOrDog(titles) { | |
| return titles.reduce((accumulator, title) => { | |
| if (title.toLowerCase().includes('cat')) { | |
| accumulator.cat++ | |
| } | |
| if (title.toLowerCase().includes('dog')) { | |
| accumulator.dog++ | |
| } | |
| return accumulator; | |
| }, { cat: 0, dog: 0}) | |
| } | |
| function selectTitles(response) { | |
| return response.data.children.map(x => x.data.title); | |
| } | |
| function getAwwPosts(numPosts) { | |
| return rp.get(`https://reddit.com/r/aww.json?limit=${numPosts}`, { json: true }); | |
| } | |
| export function getDogAndCatTitleCount(numPosts) { | |
| return getAwwPosts(numPosts) | |
| .then(selectTitles) | |
| .then(countTitlesWithCatOrDog); | |
| } |
This file contains hidden or 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
| import { getDogAndCatTitleCount } from './awwCounter'; | |
| getDogAndCatTitleCount(20) | |
| .then(x => console.log(`cats: ${x.cat}\ndogs: ${x.dog}`)); | |
| /* | |
| When I ran this, it printed: | |
| cats: 2 | |
| dogs: 5 | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment