Last active
May 25, 2019 16:35
-
-
Save mattmusc/caaf325521dc60921392750012abcaca to your computer and use it in GitHub Desktop.
Udemy - Advanced Web Developer Bootcamp - ES2015 Promises Exercises
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 $ from "jquery"; | |
| /** | |
| * Using the github developer API, returns the username | |
| * among the list of usernames passed | |
| * with the highest num of followers. | |
| * | |
| * @param {...string} names Array of Github usernames | |
| */ | |
| function getMostFollowers(...names) { | |
| const getUserWithMostFollowers = a => | |
| a.reduce( | |
| (acc, user) => { | |
| if (user.followers > acc.followers) { | |
| acc.followers = user.followers; | |
| acc.login = user.login; | |
| } | |
| return { | |
| followers: acc.followers, | |
| login: acc.login | |
| }; | |
| }, | |
| { | |
| followers: -1, | |
| login: "" | |
| } | |
| ); | |
| const parseJSON = ({ login, followers }) => { | |
| return { login, followers }; | |
| }; | |
| const getFollowersNum = user => { | |
| const url = `https://cors.io/?https://api.github.com/users/${user}`; | |
| return $.getJSON(url).then(parseJSON); | |
| }; | |
| Promise.all(names.map(name => getFollowersNum(name))).then(data => { | |
| if (!data.length) return; | |
| let { login, followers } = getUserWithMostFollowers(data); | |
| console.log(`${login} has the most followers with ${followers}`); | |
| }); | |
| } | |
| function starWarsString(number, url = 'https://swapi.co/api/people') { | |
| return $.getJSON(`${url}/${number}`).then((json) => json.name); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment