Created
August 26, 2017 13:06
-
-
Save Chiamaka/ab72cf643a37d53c50e93ad934ef09d3 to your computer and use it in GitHub Desktop.
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
const courseMap = { | |
C: ['A', 'B', 'F', 'H', 'D', 'G'], | |
B: ['K', 'L'], | |
H: ['E', 'J'], | |
D: ['A', 'B'] | |
}; | |
const learnCourse = (course) => { | |
const coursesChecked = {}; | |
const fullList = []; | |
(function learnCourseRecursive(course) { | |
const courseDep = courseMap[course]; | |
if (!coursesChecked[course]) { | |
coursesChecked[course] = true; | |
} | |
if (courseDep === undefined) { | |
fullList.push(course); | |
} | |
else { | |
for (let i=0, length=courseDep.length; i<length; i++) { | |
if (!coursesChecked[courseDep[i]]) { | |
learnCourseRecursive(courseDep[i]); | |
} | |
} | |
fullList.push(course); | |
} | |
})(course); | |
return fullList; | |
}; | |
console.log(learnCourse('C')); | |
console.log(learnCourse('A')); | |
console.log(learnCourse('H')); | |
console.log(learnCourse('Z')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment