I have an array of events where every event has an action (login/logout) and a timestamp.
const events = [
{ action: 'login', timestamp: 10 },
{ action: 'logout', timestamp: 20 },
{ action: 'login', timestamp: 55 },
{ action: 'logout', timestamp: 65 },
]
I am trying to write a function calculating the total user session length which is defined by the difference between a login and logout. And a login always comes before a logout.
function getUserSessionLength(events) {
let userSession = 0
let lastTimeLogIn = 0
for (const { action, timestamp } of events) {
if (action === 'login') lastTimeLogIn = timestamp
else userSession += timestamp - lastTimeLogIn
}
return userSession
}
This works but I wonder if there is a simpler or cleaner way to calculate it? Right now I need to use an extra variable to keep track of the last login timestamp while traversing the array.