Skip to content

Instantly share code, notes, and snippets.

@armw4
Last active August 21, 2018 23:27
Show Gist options
  • Save armw4/0df104fab860b8aa5e81 to your computer and use it in GitHub Desktop.
Save armw4/0df104fab860b8aa5e81 to your computer and use it in GitHub Desktop.
Coding exercise
import $http from 'http-as-promised'
import Promise from 'bluebird'
const resolveClass = (currentCount, { room, students, next }) => {
const studentsTwentyFiveOrOlder = students.filter(({ age }) => age > 24)
const count = currentCount + studentsTwentyFiveOrOlder.length
if (next) {
return $http
.get({
url: next,
resolve: 'body',
json: true
})
.then((nextClass) => resolveClass(count, nextClass))
} else {
return Promise.resolve({ count, room })
}
}
$http
.get({
url: 'http://challenge.broadly.com/',
resolve: 'body',
json: true
})
.then(({ url }) => {
return $http
.get({
url,
resolve: 'body',
json: true
})
})
.then(({ classes })=> {
return Promise.map(classes, (apiClass) => {
return $http
.get({
url: apiClass,
resolve: 'body',
json: true
})
})
})
.then(classes => Promise.map(classes, (apiClass) => resolveClass(0, apiClass)))
.then(classes => classes.reduce((previous, { count }) => (previous + count), 0) / classes.length)
.then((average) => console.log('the average is', average))
{
"name": "broadly",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"broadly": "babel-node index"
},
"author": "@armw4",
"license": "MIT",
"dependencies": {
"bluebird": "^3.3.4",
"http-as-promised": "^1.1.0",
"node-babel": "^0.1.2"
}
}
@armw4
Copy link
Author

armw4 commented Mar 21, 2016

Verified again node@5.9.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment