binary draft
This commit is contained in:
parent
7f9a1e2c7a
commit
973f97cc09
38 changed files with 906 additions and 312 deletions
|
@ -0,0 +1,18 @@
|
|||
function concat(arrays) {
|
||||
// sum of individual array lengths
|
||||
let totalLength = arrays.reduce((acc, value) => acc + value.length, 0);
|
||||
|
||||
if (!arrays.length) return null;
|
||||
|
||||
let result = new Uint8Array(totalLength);
|
||||
|
||||
// for each array - copy it over result
|
||||
// next array is copied right after the previous one
|
||||
let length = 0;
|
||||
for(let array of arrays) {
|
||||
result.set(array, length);
|
||||
length += array.length;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
function concat(arrays) {
|
||||
// ...your code...
|
||||
}
|
||||
|
||||
let chunks = [
|
||||
new Uint8Array([0, 1, 2]),
|
||||
new Uint8Array([3, 4, 5]),
|
||||
new Uint8Array([6, 7, 8])
|
||||
];
|
||||
|
||||
console.log(Array.from(concat(chunks))); // 0, 1, 2, 3, 4, 5, 6, 7, 8
|
||||
|
||||
console.log(concat(chunks).constructor.name); // Uint8Array
|
31
6-binary/01-arraybuffer-and-views/01-concat/_js.view/test.js
Normal file
31
6-binary/01-arraybuffer-and-views/01-concat/_js.view/test.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
describe("concat", function() {
|
||||
let chunks = [
|
||||
new Uint8Array([0, 1, 2]),
|
||||
new Uint8Array([3, 4, 5]),
|
||||
new Uint8Array([6, 7, 8])
|
||||
];
|
||||
|
||||
it("result has the same array type", function() {
|
||||
|
||||
let result = concat(chunks);
|
||||
|
||||
assert.equal(result.constructor, Uint8Array);
|
||||
});
|
||||
|
||||
it("concatenates arrays", function() {
|
||||
|
||||
let result = concat(chunks);
|
||||
|
||||
assert.deepEqual(result, new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8]));
|
||||
|
||||
});
|
||||
|
||||
it("returns empty array on empty input", function() {
|
||||
|
||||
let result = concat([]);
|
||||
|
||||
assert.equal(result.length, 0);
|
||||
|
||||
});
|
||||
|
||||
});
|
0
6-binary/01-arraybuffer-and-views/01-concat/solution.md
Normal file
0
6-binary/01-arraybuffer-and-views/01-concat/solution.md
Normal file
4
6-binary/01-arraybuffer-and-views/01-concat/task.md
Normal file
4
6-binary/01-arraybuffer-and-views/01-concat/task.md
Normal file
|
@ -0,0 +1,4 @@
|
|||
|
||||
# Concatenate typed arrays
|
||||
|
||||
Given an array of `Uint8Array`, write a function `concat(arrays)` that returns a concatenation of them into a single array.
|
Loading…
Add table
Add a link
Reference in a new issue