The question I have been given is;
Given a nested array or arrays, return a new, flat array with all the elements of all the nested arrays in their original order
The answer that I have come up with is;
function flattenArray (arrayOfArrays) {
arrays = arrays.reduce(function(a, b){
return a.concat(b);
}, []);
console.log(merged);
My answer is being tested against this;
describe("flattenArray", () => {
it("returns a flat array with all the elements of the nested arrays in their original order", () => {
let arrayOfArrays, expected;
arrayOfArrays = [[1, 2], [], [3], ["hello", true]];
expected = [1, 2, 3, "hello", true];
expect(flattenArray(arrayOfArrays)).to.eql(expected);
arrayOfArrays = [[1], [2], [[3, 4]]];
expected = [1, 2, [3, 4]];
expect(flattenArray(arrayOfArrays)).to.eql(expected);
});
it("does not mutate the passed array, i.e. returns a new array, leaving the original untouched", () => {
const original = [[1, 2], [], [3, 4]];
const flat = flattenArray(original);
expect(original).to.not.equal(flat);
expect(original).to.eql([[1, 2], [], [3, 4]]);
});
});
I have no clue how else to try and solve this question, does anyone have any sugestions.