JSFiddle - React, Tailwind, and code Playground
by kkdaily
HTML
<div>
Recommendation Engine interview problem
</div>
JavaScript
/*
You have been tasked with building a feature that displays featured recommendations to customers in our app.
This program will take in 2 parameters.
One of them being a single object containing all recommendations in the app, where each key is the name of the recommendation, and each value is a Boolean that indicates whether the user has completed that recommendation yet or not.
The 2nd parameter this program takes in is an array of strings representing featured recommendations for this user. Each string in this array contains the name of a recommendation that should be displayed.
This program should return a list of recommendations that have not yet been done AND whose name appears in the featured recommendations list, in the order specified in that list.
*/
// featuredRecommendations = ['dog', 'cat', 'horse']
// recommendations = { 'horse': T, 'cat': F, 'dog': F }
const recommendationEngine = (recommendations, featuredRecommendations) => {
// convert recommendations to an array of objects for better parsing
const orderedRecommendations = [];
featuredRecommendations.forEach((featuredRecName) => {
// BONUS: what if the featured list of recs is out of date and ends up referencing a non-existant rec?
const recommendationExists = featuredRecName in recommendations;
const recommendationCompleted = recommendations[featuredRecName];
if (recommendationExists && !recommendationCompleted) {
orderedRecommendations.push({
name: featuredRecName,
completed: recommendationCompleted
});
}
});
return orderedRecommendations;
};
var result = recommendationEngine({'horse': true, 'cat': false, 'dog': false}, ['dog', 'cat', 'horse', 'chicken']);
alert(JSON.stringify(result));