function anas(array, arraysCount) {
let arrayLength = array.length;
if( arrayLength == 0 ) {
return array;
}
if( arraysCount > arrayLength ) {
return array;
}
let result = [];
let newArray = [];
const newArraysLength = arrayLength % arraysCount == 0 ? Math.floor( arrayLength / arraysCount) : Math.ceil ( arrayLength / arraysCount);
for( let i = 0 ; i < arrayLength; i++ ) {
if( i % newArraysLength == 0 ) {
if( i > 0 ) {
result.push( newArray );
newArray = [];
}
}
newArray.push( array[i] );
}
let canBeSplitEqually = ( ( arrayLength ) % newArraysLength == 0 );
console.log("canBeSplitEqually = ", canBeSplitEqually);
if( !( canBeSplitEqually ) ) {
result.push( newArray );
}
return result;
}
function nabil (
array,
n
) {
const elementsPerGroup = Math.round(array.length / n);
let result = [];
let positionIndex = 0;
for (let i = 1; n >= i; i++) {
const lastIndex =
i === n
? array.length - positionIndex + positionIndex
: elementsPerGroup + positionIndex;
result.push(array.slice(positionIndex, lastIndex));
positionIndex = positionIndex + elementsPerGroup;
}
return result;
};