Hi im doing a job at work where we have a camera system that pings everytime someone enters or exits a room so you would have data like this:
{enter:7, exit:6}
which should mean one person is left in room
I want to know what was the highest amount of people in the room over a given period lets just say 45 minutes. I could have 50 to 100 points of data how can i would out what the average would be
Thanks
// update with a code sample here
.then((results) => {
// maybe a correct algorithim
results.sort((a, b) => b.enter - a.enter);
results.sort((a, b) => b.exit - a.exit);
results.sort((a, b) => a.enter - b.enter || b.exit - a.exit);
// results.sort(fieldSorter(["enter"]));
let final_result = result.pop();
console.log(final_result);
if (final_result) {
return parseInt(final_result.enter - final_result.exit);
} else {
return 0;
}
})
So before this code im gathering all the datapoints then try and sort them and pop off the highest entertence to the lowest exits but i think im going about this wrong
Assuming that enter time is always smaller than exit time (which contradicts your example data), the issue could be solved like this:
function getAverage(startTime, endTime, items) {
let slots = {};
for (let timeIndex = startTime; timeIndex <= endTime; timeIndex++) {
slots[timeIndex] = 0;
}
for (item of items) {
let enter = Math.min(item.enter, startTime);
let exit = Math.min(item.exit, endTime);
for (let momentIndex = enter; momentIndex <= exit; momentIndex++) {
slots[momentIndex]++;
}
}
let avg = 0;
for (let slot of slots) avg += slot;
return avg / slots.length;
}
EDIT
For computing the maximum you can do something like this:
function getAverage(startTime, endTime, items) {
let slots = {};
for (let timeIndex = startTime; timeIndex <= endTime; timeIndex++) {
slots[timeIndex] = 0;
}
for (item of items) {
let enter = Math.min(item.enter, startTime);
let exit = Math.min(item.exit, endTime);
for (let momentIndex = enter; momentIndex <= exit; momentIndex++) {
slots[momentIndex]++;
}
}
let max = 0;
for (let slot of slots) max = Math.max(max, slot);
return max;
}