Last active
August 21, 2018 23:27
-
-
Save armw4/0df104fab860b8aa5e81 to your computer and use it in GitHub Desktop.
Coding exercise
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
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)) |
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": "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" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Verified again
node@5.9.0