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 b24515a

Browse files
Merge pull request #36 from somekindofwallflower/feature/array_chunking
Implement the chunk array to a given size problem solutions, write tests
2 parents f6cefcc + 8204659 commit b24515a

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

‎06_array_chunking/index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
// Steps
12+
// Create empty array to hold chunks called 'chunked'
13+
// Loop through each element in the unchanked array
14+
// Retrieve the last element in the chunked
15+
// If element does not exist or the element length is equal to chunk size push e new chunk to the chunked array with the element
16+
// Else add the element into the chunk
17+
18+
19+
// Solution 1
20+
// function chunk(array, size) {
21+
// let chunked = [];
22+
// for (let element of array) {
23+
// const last = chunked[chunked.length - 1]
24+
// if(!last || last.length === size) {
25+
// chunked.push([element])
26+
// } else {
27+
// last.push(element);
28+
// }
29+
// }
30+
// return chunked;
31+
// }
32+
33+
34+
// Steps
35+
// Create an empty chunk array
36+
// Create 'index' starts at 0
37+
// While index is less than array.length
38+
// Push a slice of length size from array into chunked
39+
// Add size to index
40+
41+
// Solution 2
42+
function chunk(array, size) {
43+
let chunked = [];
44+
let index = 0;
45+
for (let el of array) {
46+
if(index < array.length) {
47+
chunked.push(array.slice(index, index + size))
48+
index = index + size;
49+
}
50+
}
51+
return chunked;
52+
}
53+
54+
55+
module.exports = chunk;

‎06_array_chunking/test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const chunk = require('./index');
2+
3+
test('function chunk exists', () => {
4+
expect(typeof chunk).toEqual('function');
5+
});
6+
7+
test('chunk divides an array of 10 elements with chunk size 2', () => {
8+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
9+
const chunked = chunk(arr, 2);
10+
11+
expect(chunked).toEqual([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]);
12+
});
13+
14+
test('chunk divides an array of 3 elements with chunk size 1', () => {
15+
const arr = [1, 2, 3];
16+
const chunked = chunk(arr, 1);
17+
18+
expect(chunked).toEqual([[1], [2], [3]]);
19+
});
20+
21+
test('chunk divides an array of 5 elements with chunk size 3', () => {
22+
const arr = [1, 2, 3, 4, 5];
23+
const chunked = chunk(arr, 3);
24+
25+
expect(chunked).toEqual([[1, 2, 3], [4, 5]]);
26+
});
27+
28+
test('chunk divides an array of 13 elements with chunk size 5', () => {
29+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
30+
const chunked = chunk(arr, 5);
31+
32+
expect(chunked).toEqual([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13]]);
33+
});

0 commit comments

Comments
(0)

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