const CleanTable = [{seat: 1, group: 0}, {seat: 2, group: 0}, {seat: 3, group: 0}, {seat: 4, group: 0}, {seat: 5, group: 0}];
function groupCalculator(Table, Group, Index) {
const emptySpace = Table.filter((s) => s.group === 0)
const occupied = Table.filter((s) => s.group !== 0);
if (Group > emptySpace.length) return "no Free Slots";
else {
const fillSeats = emptySpace.slice(0, Group).reduce((newArr, current) => {
const { seat } = current;
const object = { seat: seat, group: Index };
const arr = [object, ...newArr];
return arr.sort((a, b) => a.seat > b.seat);
}, []);
const slice = Table.slice(Group);
const merge = [...fillSeats, ...slice];
return merge ;
}
}
I am currently working on an algorithm that assigns Slots A to a group B. When the Slots are occupied, they are marked with a group ID. If the group leaves their seats, they can be reassigned.
My idea was to use an array of objects for this purpose and fill it up sequentially.
In the first round everything works fine (https://jsfiddle.net/5b3a1try/14/ "Code e Exampel") ), but after the second round nothing works anymore.
Am I on the right track here, or am I going about this the wrong way?
I use this function in a React app, which means that the Executiontriggered via a forum.