Как разделить этот массив на n равных частей?
если 1-100 - это предоставленный ввод, я хочу, чтобы вывод был в кусках по 10, отображаемых в отдельных строках.
function range(start, end) {
var ans = [];
for (let i = start; i <= end; i++) {
ans.push(i);
}
return ans;
}



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


С этим:
Array.prototype.chunk = function ( n ) {
if ( !this.length ) {
return [];
}
return [this.slice(0, n)].concat(this.slice(n).chunk(n));
};
А потом:
const splittendAns = ans.chunk(20);
В последней строке вы делите массив на куски длины 20.
Вот пример по запросу:
// Suppose I have this array
// I want to split this array in 5 length arrays
const array = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
];
Array.prototype.chunk = function ( n ) {
if ( !this.length ) {
return [];
}
return [this.slice(0, n)].concat(this.slice(n).chunk(n));
};
const splittedArray = array.chunk(5);
console.info(array);
console.info('-----');
console.info(splittedArray);
ВЫХОД:
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]
-----
[ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8, 9, 10 ], [ 11, 12, 13, 14, 15 ] ]