Here, which is processed when all words are filled. The sample code returns -1, but you can update it to return something.
function findRandomDayIndex() {
var m = _.map(dayslots, function(v, k) {
if (!v.filled) {
return k;
}
});
m = _.without(m, undefined);
if (m.length === 0) {
return -1;
} else {
return m[_.random(0, m.length)];
}
}
var dayslots = [];
for (var i = 0; i < 40; i++) {
dayslots.push({
filled: false
});
}
console.log(findRandomDayIndex());
If you change the filled to true, you will get -1. A.
source
share