(scenario) I have an array with N number of animals, and I have to distribute with a range between 4 and 5 every day for 6 days. So the range for entire week can be 28 => 30.
axample:
var animals = ["Antelope", "Bisont", "Crocodile", "Dingo","Elephant","Fly","Gnu","Hyena","Porcupine","Llama","Macaque","Nasica","Orangutan","Piton","Quetzal","Rinho"];
animals = 16, min animals request = 28
this snippet below solve two problems: chunk as explained here and randomize as explained here:
i need to improve groupsize variable from fixed value to range value.
var animals = ["Antelope", "Bisont", "Crocodile", "Dinosaur","Elephant","Fly","Gnu",
"Hyena","Porcupine","Llama","Macaque","Nasica","Orangutan","Piton","Quetzal","Rinho"];
var groupSize = 4; // here need range
var groups = _.map(animals, function(item, index){
return index % groupSize === 0 ? animals.slice(index, index + groupSize) : null;
})
.filter(function(item){ return item;
});
function shuffle(array) {
var m = array.length, t, i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m-- );
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
console.log(shuffle(groups));
console
[["Porcupine", "Llama", "Macaque", "Nasica"], ["Antelope", "Bisont", "Crocodile", "Dinosaur"], ["Elephant", "Fly", "Gnu", "Hyena"], ["Orangutan", "Piton", "Quetzal", "Rinho"]]
chunk and shuffle are good, but at least 6 are needed and have 4. For the remining two some animals must do a second turn.
So the question is: how i can fill Array[6] whit chunk of minimum of 4 shuffled animals? Any suggestion for find right path is is widely appreciated.