-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorganizeIntructors.js
34 lines (33 loc) · 1.16 KB
/
organizeIntructors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const organizeInstructors = function(instructors) {
let courses = [instructors[0].course]; // loads first index
let answerObject = {}; // initiate pointer to answer;
answerObject[courses[0]] = [instructors[0].name]; // initial course
let find = ''; // if false FIND in index adds a new attribute
for(let i = 1 ; i < instructors.length ; i++){
find = false;
for(let c = 0; c < courses.length ; c++){
if(courses[c] == instructors[i].course){
answerObject[courses[c]].push(instructors[i].name);
find = true; // If course in the list just add instructor
break;
}
}
if(find == false){ //course not in the list create new attribute
courses[i] = (instructors[i].course);
answerObject[courses[i]] = [instructors[i].name];
}
}
return answerObject;
};
console.log(organizeInstructors([
{name: "Samuel", course: "iOS"},
{name: "Victoria", course: "Web"},
{name: "Karim", course: "Web"},
{name: "Donald", course: "Web"}
]));
console.log(organizeInstructors([
{name: "Brendan", course: "Blockchain"},
{name: "David", course: "Web"},
{name: "Martha", course: "iOS"},
{name: "Carlos", course: "Web"}
]));