Skip to content

Commit bc4d667

Browse files
committed
add 4-binary
1 parent f0233c4 commit bc4d667

20 files changed

+784
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function concat(arrays) {
2+
// sum of individual array lengths
3+
let totalLength = arrays.reduce((acc, value) => acc + value.length, 0);
4+
5+
if (!arrays.length) return null;
6+
7+
let result = new Uint8Array(totalLength);
8+
9+
// for each array - copy it over result
10+
// next array is copied right after the previous one
11+
let length = 0;
12+
for(let array of arrays) {
13+
result.set(array, length);
14+
length += array.length;
15+
}
16+
17+
return result;
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function concat(arrays) {
2+
// ...your code...
3+
}
4+
5+
let chunks = [
6+
new Uint8Array([0, 1, 2]),
7+
new Uint8Array([3, 4, 5]),
8+
new Uint8Array([6, 7, 8])
9+
];
10+
11+
console.log(Array.from(concat(chunks))); // 0, 1, 2, 3, 4, 5, 6, 7, 8
12+
13+
console.log(concat(chunks).constructor.name); // Uint8Array
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
describe("concat", function() {
2+
let chunks = [
3+
new Uint8Array([0, 1, 2]),
4+
new Uint8Array([3, 4, 5]),
5+
new Uint8Array([6, 7, 8])
6+
];
7+
8+
it("result has the same array type", function() {
9+
10+
let result = concat(chunks);
11+
12+
assert.equal(result.constructor, Uint8Array);
13+
});
14+
15+
it("concatenates arrays", function() {
16+
17+
let result = concat(chunks);
18+
19+
assert.deepEqual(result, new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8]));
20+
21+
});
22+
23+
it("returns empty array on empty input", function() {
24+
25+
let result = concat([]);
26+
27+
assert.equal(result.length, 0);
28+
29+
});
30+
31+
});

4-binary/01-arraybuffer-binary-arrays/01-concat/solution.md

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
# Concatenate typed arrays
3+
4+
Given an array of `Uint8Array`, write a function `concat(arrays)` that returns a concatenation of them into a single array.
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)