skill1Results = {
const iterationNumber = Math.max(...studentSkill1Choices.choices.map(x => x.choices.length))
let results = []
let subjectsStatus = []
const shuffledChoices = _.shuffle(studentSkill1Choices.choices)
for (let x = 0; x < iterationNumber; x++) {
for (let { studentId, choices } of shuffledChoices) {
const studentResultIndex = results.findIndex(y => y.studentId === studentId)
const subjectStatusIndex = subjectsStatus.findIndex(y => y.subjectId === choices[x])
if (studentResultIndex === -1) {
if (subjectStatusIndex === -1) {
results = [
...results,
{
studentId,
choices: [choices[x]]
}
]
subjectsStatus = [
...subjectsStatus,
{
subjectId: choices[x],
students: 1
}
]
} else {
if (subjectsStatus[subjectStatusIndex].students < skillSubjects.find(y => y.id === choices[x]).capacity) {
results = [
...results,
{
studentId,
choices: [choices[x]]
}
]
subjectsStatus = [
...subjectsStatus.filter(x => x.subjectId !== subjectsStatus[subjectStatusIndex].subjectId),
{
...subjectsStatus[subjectStatusIndex],
students: subjectsStatus[subjectStatusIndex].students +1
}
]
}
}
} else {
if (results[studentResultIndex].choices.length < currentSkill.optionalSubjectsNb) {
if (subjectStatusIndex === -1) {
results = [
...results.filter(x => x.studentId !== results[studentResultIndex].studentId),
{
...results[studentResultIndex],
choices: [...results[studentResultIndex].choices, choices[x]]
}
]
subjectsStatus = [
...subjectsStatus,
{
subjectId: choices[x],
students: 1
}
]
} else {
if (subjectsStatus[subjectStatusIndex].students < skillSubjects.find(y => y.id === choices[x]).capacity) {
results = [
...results.filter(x => x.studentId !== results[studentResultIndex].studentId),
{
...results[studentResultIndex],
choices: [...results[studentResultIndex].choices, choices[x]]
}
]
subjectsStatus = [
...subjectsStatus.filter(x => x.subjectId !== subjectsStatus[subjectStatusIndex].subjectId),
{
...subjectsStatus[subjectStatusIndex],
students: subjectsStatus[subjectStatusIndex].students +1
}
]
}
}
}
}
}
}
return { results, subjectsStatus }
}