Hello to all community members. I have to deal with new challeng in javascript and i ask help to win.
Description
I have an array of objects containing students. The students have different attend like year 1 and so on until year 4.
var arrayStudents = [
{student_id: "11", student_name: "Messina", student_year_id: "1"},
{student_id: "6", student_name: "Di Ruzza", student_year_id: "1"},
{student_id: "17", student_name: "Simonetti", student_year_id: "1"},
{student_id: "14", student_name: "Ciaglia", student_year_id: "1"},
{student_id: "37", student_name: "Polito", student_year_id: "2"},
{student_id: "31", student_name: "Izzo", student_year_id: "2"},
{student_id: "45", student_name: "Cilia", student_year_id: "3"},
{student_id: "69", student_name: "Kutrolli", student_year_id: "4"},
{student_id: "71", student_name: "Lopez", student_year_id: "4"}
];
//console.log(arrayStudents.length);
//console.log(arrayStudents);
for this example the array contain 9 students: 4 of 1 year, 2 of 2 year, 1 of 3 year and 2 of 4 year.
So arrayT.length = 9
Now with this array a need to create a matrix array: Width = arrayT.lenght, Height 24
letâs create
function createGround(width) {
var clonearrayStudents = arrayStudents.slice();
// clone are only my test i can not, but want to do
var result = [];
for (var i = 0; i < width; i++) {
result[i] = clonearrayStudents
}
return result;
}
var groundx = createGround(24);
Well done! Now i have 2D array.
- reasoning
Here i need to del with this delicious data for a destination goal.
for now i have matrix array (2D) lenght 24 and width 9. - But any width on the matrix is the same. that is, it contains the same arry for each index. So now I need to shuffle each array by index.
For this we use the Fisher and Yates algorithm. So
shuffleBlock(groundx);
function shuffleBlock (ground){
for (var y = 0; y < ground.length; y++){
console.log(y);
var cube = shufflex(ground[y]);
var content =[];
for(var j = 0; j < cube.length; j++) {
console.log("cube[" + y + "][" + j + "] = "+ typeof cube[j] + JSON.stringify(cube[j]));
content.push(cube[j]);
}
console.log('----------------');
console.log(content);
}
//console.log(ground);
// Fisher and Yates algorithm
function shufflex(array) {
var tmp, current, top = array.length;
if (top)
while (--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array;
}
Very nice output: Thank you Fisher and Yates algorithm!
- reasoning
At this step i heve what i desired, but come up a Problem where i hask to help from some expert.
The problem
look at console output of index 0 of the matrix array:
0
cube[0][0] = object{"student_id":"71","student_name":"Lopez Mariangela","student_year_id":"4"}
cube[0][1] = object{"student_id":"6","student_name":"Di Ruzza Alberto","student_year_id":"1"}
cube[0][2] = object{"student_id":"17","student_name":"Simonetti Stefania","student_year_id":"1"}
cube[0][3] = object{"student_id":"37","student_name":"Polito Eleonora","student_year_id":"2"}
cube[0][4] = object{"student_id":"31","student_name":"Izzo Antonella","student_year_id":"2"}
cube[0][5] = object{"student_id":"11","student_name":"Messina Emanuele","student_year_id":"1"}
cube[0][6] = object{"student_id":"45","student_name":"Cilia Francesco","student_year_id":"3"}
cube[0][7] = object{"student_id":"69","student_name":"Kutrolli Eriselda","student_year_id":"4"}
cube[0][8] = object{"student_id":"14","student_name":"Ciaglia Simone","student_year_id":"1"}
For my my project it is important that first year students are not consequent (like position [0][1] and [0][2]) and if they are, it is necessary to exchange positions with another index where the student_year_id property is greater than one like below
cube[0][0] = object{"student_id":"71","student_name":"Lopez Mariangela","student_year_id":"4"}
cube[0][1] = object{"student_id":"6","student_name":"Di Ruzza Alberto","student_year_id":"1"}
cube[0][3] = object{"student_id":"37","student_name":"Polito Eleonora","student_year_id":"2"}
cube[0][2] = object{"student_id":"17","student_name":"Simonetti Stefania","student_year_id":"1"}
cube[0][4] = object{"student_id":"31","student_name":"Izzo Antonella","student_year_id":"2"}
cube[0][6] = object{"student_id":"45","student_name":"Cilia Francesco","student_year_id":"3"}
cube[0][5] = object{"student_id":"11","student_name":"Messina Emanuele","student_year_id":"1"}
cube[0][7] = object{"student_id":"69","student_name":"Kutrolli Eriselda","student_year_id":"4"}
cube[0][8] = object{"student_id":"14","student_name":"Ciaglia Simone","student_year_id":"1"}
HELP
i need to create a function with a loop inside for any index of the array and if find conseguent property move change position to reflect my needed.
So please help me to find the right path
Thank you in advance