Created
February 5, 2019 19:19
-
-
Save pgangwani/3fbd6b9865d69b7b6423695d43cbc206 to your computer and use it in GitHub Desktop.
Zalando - Coding Test
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
| 'use strict'; | |
| /* global CustomError, getLikedBrands, getTopBrandsForGender */ | |
| function solution(U, N) { | |
| return new Promise((resolve, reject) => { | |
| // Uniq function | |
| const uniq = list => Array.from( new Set(list) ) | |
| getLikedBrands(U.id).then(likedBrands => likedBrands.length >= N | |
| // If liked brands are enough | |
| ? resolve( | |
| likedBrands | |
| .slice(0, N) | |
| .map(i => i.name) | |
| ) | |
| // If not enough, get more brands | |
| : getTopBrandsForGender(U.gender).then(topBrands => { | |
| // Concat and unify brands list | |
| const allBrands = uniq( | |
| [...likedBrands, ...topBrands] | |
| .map(i => i.name) | |
| ) | |
| allBrands.length >= N | |
| // If all brands are enough | |
| ? resolve(allBrands.slice(0, N)) | |
| // If still not enough | |
| : reject(new CustomError('Not enough data')) | |
| }) | |
| ) | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment