Skip to content

Instantly share code, notes, and snippets.

@denisos
Created August 10, 2022 23:37
Show Gist options
  • Save denisos/071491026bd2b1435404699e37e168c5 to your computer and use it in GitHub Desktop.
Save denisos/071491026bd2b1435404699e37e168c5 to your computer and use it in GitHub Desktop.
const berries = ["blackberry", "raspberry", "strawberry"];
const citrus = ["orange", "lime", "mandarin"];
const melons = ["watermelon", "honeydew", "cantalopue"]
let list = [
{
name: "watermelon"
},
{
name: "orange"
},
{
name: "strawberry"
},
{
name: "lime"
},
{
name: "honeydew"
},
{
name: "mandarin"
},
{
name: "blackberry"
},
]
// @param item
// @returns: number
function rank(item) {
if (berries.includes(item.name)) return 1;
if (citrus.includes(item.name)) return 2;
return 3;
}
// @param []
// @param callback
// @returns: []
function ranker(items, rankCallback) {
const rankedItems = items.map(item => ({
item,
rank: rankCallback(item)
}));
rankedItems.sort((a,b) => a.rank - b.rank);
return rankedItems.map(item => item.item)
}
console.log("ranker result : ", ranker(list, rank))
// goal: this code runs and is fine as is (no changes needed)
// but we're adopting typescript and want you to refactor it to use typescript
// please add types etc for the functions and params
//.(we want to better understand your ts skills)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment