Created
August 19, 2024 12:42
-
-
Save varenya/f5d31a357bd8ade6b1328327e479ef4f 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
type Task = { | |
name: string; | |
time: number; | |
event: "start" | "end"; | |
}; | |
function calculateExecutionTimes(tasks: Task[]) { | |
const initialTaskMap: { [name: string]: { start: number; end: number } } = {}; | |
const groupedTasks = tasks.reduce((acc, curr) => { | |
const currValue = acc[curr.name] || {}; | |
if (curr.event == "start") { | |
return { ...acc, [curr.name]: { ...currValue, start: curr.time } }; | |
} else { | |
return { ...acc, [curr.name]: { ...currValue, end: curr.time } }; | |
} | |
}, initialTaskMap); | |
const taskValues = Object.keys(groupedTasks).map((task) => { | |
const timeTaken = groupedTasks[task]; | |
return { [task]: timeTaken.end - timeTaken.start }; | |
}); | |
return taskValues; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment