-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk.js
35 lines (33 loc) · 928 Bytes
/
chunk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// --- Directions
// Given a string, return true if the string is a palindrome
// or false if it is not. Palindromes are strings that
// form the same word if it is reversed. *Do* include spaces
// and punctuation in determining if the string is a palindrome.
// --- Examples:
// palindrome("abba") === true
// palindrome("abcdefg") === false
function chunk(array, size) {
const newArr = [];
let counter = 0;
while (counter < array.length) {
newArr.push(array.slice(counter, counter + size));
counter += size;
}
return newArr
}
chunk([1, 2, 3, 4], 2) // --> [[ 1, 2], [3, 4]]
// function chunk(array, size) {
// const newArr = [];
//
// for (let element of array) {
// const last = newArr[newArr.length - 1];
//
// if (!last || last.length === size) {
// newArr.push([element]);
// } else {
// last.push(element);
// }
// }
//
// return newArr;
// }