Created
July 23, 2021 20:55
-
-
Save KravMaguy/382de42bbc2259ebbf26df925b85a0fd to your computer and use it in GitHub Desktop.
This file contains 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
// Coding exercise: | |
// Complete the function getLessonsByStatus. | |
// That function should return all lessons grouped by the progress status for the requested userId. | |
// There is a commented example of what the function might return. | |
// You will get data from the getLessons and getProgress function. | |
// There are commented examples of what each function might return on success. | |
// Please explain your logic as you write code. | |
// The code does not need to run, but should be logically sound. | |
// const { get } = require("axios"); | |
// async function getLessons() { | |
// const response = await get("/lessons"); | |
// return response.data; | |
// // Returns [ { lessonId: 1, title: 'NM 101' }, { lessonId: 2, title: 'NM 102' } ] | |
// } | |
// async function getProgress(userId) { | |
// const response = await get(`/progress?userId=${userId}`); | |
// return response.data; | |
// // Returns [ { lessonId: 1, userId: 1, status: 'completed' } ] | |
// // status can be 'completed', 'failed', 'in progress' | |
// // No progress returned for a lesson that was not started | |
// } | |
async function getLessonsByStatus(userId) { | |
// That function should return all lessons grouped by the progress status for the requested userId. | |
// Returns lesson categorized by status | |
// const aggregatedLessons; | |
// const lessons= await getLessons() | |
// lessons.then(allLessons=> | |
// aggregatedLessons=[...allLessons]) | |
// const progressObjects= await getProgress(userId) | |
// progressObjects.then(progressArray =>{ | |
// progressArray.map(obj=>{ | |
// if(obj.lessonId) | |
// }) | |
// }) | |
const result = { | |
completed: [], | |
failed: [], | |
inProgress: [], | |
notStarted: [], | |
}; | |
const aggregatedLessons = [ | |
{ lessonId: 1, title: "NM 101" }, | |
{ lessonId: 2, title: "NM 102" }, | |
{ lessonId: 3, title: "NM 202" }, | |
{ lessonId: 4, title: "NM 402" }, | |
]; | |
const progressObjects = [ | |
{ lessonId: 1, userId: 1, status: "completed" }, | |
{ lessonId: 2, userId: 1, status: "failed" }, | |
{ lessonId: 3, userId: 1, status: "failed" }, | |
]; | |
// Example format { | |
// completed: [ { lessonId: 1, title: 'NM 101' } ], | |
// failed: [], | |
// inProgress: [], | |
// notStarted: [ { lessonId: 2, title: 'NM 102' } ] | |
// } | |
aggregatedLessons.forEach((lesson) => { | |
const relevantProgress = progressObjects.find( | |
(progress) => progress.lessonId === lesson.lessonId | |
); | |
if (relevantProgress) { | |
result[relevantProgress.status].push(lesson); | |
} else { | |
result.notStarted.push(lesson); | |
} | |
}); | |
return JSON.stringify(result); | |
} | |
console.log(getLessonsByStatus(1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment