Matan Exercise
by samur3
JavaScript
/*
יש לך רשימה של תפקידים של עובד בחברה (לא ממוינים לפי תאריך):
Role[] history = {
new Role("Fullstack developer", LocalDate.of(2016, 5, 15)),
new Role("Team Leader", LocalDate.of(2020, 8, 1)),
new Role("Frontend developer", LocalDate.of(2015, 11, 17))
};
יש לך רשימה של תאריכים (גם לא ממוינים):
LocalDate[] dates = {
LocalDate.of(2014, 1, 1),
LocalDate.of(2021, 1, 1),
LocalDate.of(2020, 10, 15),
LocalDate.of(2016, 6, 1),
};
אתה צריך להחזיר מפה של Map<LocalDate, Role> שתיתן לכל תאריך את התפקיד שבו העובד עבד בו באותו תאריך
*/
//new Date(year, month, day, hours, minutes, seconds, milliseconds)
let historyRoles = [];
let role1 = {
name: "Fullstack developer",
date: new Date(2016,5,15,8,0,0,0)
}
let role2 = {
name: "Team Leader",
date: new Date(2020,8,1,8,0,0,0)
}
let role3 = {
name: "Frontend developer",
date: new Date(2015,11,17,8,0,0,0)
}
historyRoles.push(role1);
historyRoles.push(role2);
historyRoles.push(role3);
let dates = [new Date(2014,1,1,8,0,0,0),
new Date(2021,1,1,8,0,0,0),
new Date(2020,10,15,8,0,0,0),
new Date(2014,6,1,8,0,0,0)];
function compareDates(a,b) {
let comparison = 0;
if (a > b) {
comparison = 1;
} else if (a < b) {
comparison = -1;
}
return comparison;
};
function compare(propertyName) {
return function(a,b){
const objA = a[propertyName];
const objB = b[propertyName];
let comparison = 0;
if (objA > objB) {
comparison = 1;
} else if (objA < objB) {
comparison = -1;
}
return comparison;
}
}
function getRelevantRoles(historyRoles,dates){
if(historyRoles.length === 0 || dates.length === 0) return null;
historyRoles.sort(compare("date"));
dates.sort(compareDates);
let datesLen = dates.length;
let result = {};
let dateIndex = 0
historyRoles.forEach((role,roleIndex) => {
while(datesLen > dateIndex && (dates[dateIndex] <...