Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 53b1aac

Browse files
Adding arrayChunk
1 parent f18a19e commit 53b1aac

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

‎arrayChunk/arrayChunk.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// --- Directions
2+
// Given an array and chunk size, divide the array into many subarrays
3+
// where each subarray is of length size
4+
// --- Examples
5+
// chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]
6+
// chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]]
7+
// chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]]
8+
// chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]]
9+
// chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]
10+
11+
// Solution No. 1
12+
13+
function arrayChunk(array, size) {
14+
let chunked = [];
15+
16+
for(let element of array) {
17+
let lastElement = chunked[chunked.length -1];
18+
19+
if(!lastElement || chunked.length === size) {
20+
chunked.push([element]);
21+
} else {
22+
lastElement.push(element);
23+
}
24+
}
25+
26+
return chunked;
27+
}
28+
29+
// Solution No. 2
30+
31+
function arrayChunk(array, size) {
32+
let chunked = [];
33+
let index = 0;
34+
while(index < array.length) {
35+
chunked.push(array.slice(index, index+size));
36+
index += size;
37+
}
38+
39+
return chunked
40+
}

‎arrayChunk/directions.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# --- Directions
2+
Given an array and chunk size, divide the array into many subarrays where each subarray is of length size
3+
4+
# --- Examples
5+
6+
* chunk([1, 2, 3, 4], 2) should return [[ 1, 2], [3, 4]]
7+
* chunk([1, 2, 3, 4, 5], 2) should return [[ 1, 2], [3, 4], [5]]
8+
* chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) should return [[ 1, 2, 3], [4, 5, 6], [7, 8]]
9+
* chunk([1, 2, 3, 4, 5], 4) should return [[ 1, 2, 3, 4], [5]]
10+
* chunk([1, 2, 3, 4, 5], 10) should return [[ 1, 2, 3, 4, 5]]
11+
12+
# --- Solution
13+
14+
Solution No. 1
15+
16+
function arrayChunk(array, size) {
17+
let chunked = [];
18+
19+
for(let element of array) {
20+
let lastElement = chunked[chunked.length -1];
21+
22+
if(!lastElement || chunked.length === size) {
23+
chunked.push([element]);
24+
} else {
25+
lastElement.push(element);
26+
}
27+
}
28+
29+
return chunked;
30+
}
31+
32+
Solution No. 2
33+
34+
function arrayChunk(array, size) {
35+
let chunked = [];
36+
let index = 0;
37+
while(index < array.length) {
38+
chunked.push(array.slice(index, index+size));
39+
index += size;
40+
}
41+
42+
return chunked
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /