I have one lab where is given:
const names = ['Peter', 'Andrew', 'Ann', 'Mark', 'Josh', 'Sandra', 'Cris', 'Bernard', 'Takesi'];
It's required to create function that will create array which will contain other arrays with three names:
`[['Peter', 'Andrew', 'Ann'], ['Mark', 'Josh', 'Sandra'], ['Cris', 'Bernard', 'Takesi']]`
I did below and it gives me one array , as expected:
function sortByGroups() {
let arr = [];
for (let i = 0; i < names.length; i = i + 3){
for (let j = i; j < i + 3; j++){
if (j < i + 2){
arr += `${names[j]},`;
} else {
arr += `${names[j]}`;
}
}
return arr.split(',')
}
}
console.log(sortByGroups()); // [ 'Peter', 'Andrew', 'Ann' ]
but after I don't know what to do to get the required result:
[['Peter', 'Andrew', 'Ann'], ['Mark', 'Josh', 'Sandra'], ['Cris', 'Bernard', 'Takesi']]