function createGroups(students) {
if (students.length % 4 !== 0) {
console.error('The number of students must be a multiple of 4 for this to work.');
return;
}
const numGroups = students.length / 4;
const groups = new Array(numGroups).fill(null).map(() => []);
let rotatedStudents = students.slice();
for (let round = 1; round < students.length; round++) {
for (let groupIndex = 0; groupIndex < numGroups; groupIndex++) {
const student = rotatedStudents.pop();
groups[groupIndex].push(student);
}
// Rotate students for the next round
rotatedStudents = [rotatedStudents.pop()].concat(rotatedStudents);
}
return groups;
}
const students = ['Student1', 'Student2', 'Student3', 'Student4', 'Student5', 'Student6', 'Student7', 'Student8', 'Student9', 'Student10', 'Student11', 'Student12', 'Student13', 'Student14', 'Student15', 'Student16', 'Student17', 'Student18', 'Student19', 'Student20', 'Student21', 'Student22', 'Student23', 'Student24'];
const groups = createGroups(students);
console.log(groups);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.