Skip to content

Instantly share code, notes, and snippets.

@zimejin
Created March 16, 2020 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zimejin/9b351d70425b594f26e4b0042265d7d2 to your computer and use it in GitHub Desktop.
Save zimejin/9b351d70425b594f26e4b0042265d7d2 to your computer and use it in GitHub Desktop.
Zalando - Coding Test
'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'))
})
)
});
}
@daanishraj
Copy link

But what if the the getLikedBrands(id) promise is rejected? You still need to check the getTopBrandsForGender(gender) Promise and return an array if the second api call returns some data. At the moment, you are only checking that promise if the first promise is resolved.

Did this solution pass all the test cases?

@zimejin
Copy link
Author

zimejin commented Apr 15, 2021

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