Skip to content

Instantly share code, notes, and snippets.

@microsoftly
Last active November 29, 2018 16:44
Show Gist options
  • Select an option

  • Save microsoftly/e19bb379b9c44f1cfdb0433f1cb71fdc to your computer and use it in GitHub Desktop.

Select an option

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
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);
}
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