function distributeBooks(books, numPeople, numTimes) {
if (numPeople <= 0 || numTimes <= 0 || books.length < numPeople * numTimes) {
return "Invalid input!";
}
const bookList = [...books];
const bookCount = bookList.length;
const avgBooks = Math.floor(bookCount / numPeople);
let extraBooks = bookCount % numPeople;
const result = [];
let startIndex = 0;
let endIndex = 0;
for (let i = 0; i < numPeople; i++) {
result[i] = [];
endIndex += avgBooks;
if (extraBooks > 0) {
endIndex++;
extraBooks--;
}
for (let j = startIndex; j < endIndex; j++) {
const bookIndex = Math.floor(Math.random() * bookList.length);
result[i].push(bookList[bookIndex]);
bookList.splice(bookIndex, 1);
}
startIndex = endIndex;
}
return result;
}
var books = ['a', 'b', 'c', 'd', 'e', 'f', 'iii', 'jjj', 'kkk', 'lll'];
var numPeople = 8;
var numTimes = 1;
var result = distributeBooks(books, numPeople, numTimes);
console.log(result);
console