The problem
Write a perform that when given a quantity >= 0, returns an Array of ascending size subarrays.
pyramid(0) => [ ]
pyramid(1) => [ [1] ]
pyramid(2) => [ [1], [1, 1] ]
pyramid(3) => [ [1], [1, 1], [1, 1, 1] ]
Notice: the subarrays ought to be stuffed with 1s
The answer in Javascript
Choice 1:
perform pyramid(n) {
const res = [];
for(let i = 0; i < n; i++){
res.push([...Array(i+1)].fill(1))
}
return res;
}
Choice 2:
perform pyramid(n) {
return Array(n).fill().map((e,i)=>Array(i+1).fill(1))
}
Choice 3:
const pyramid = n => Array(n).fill(1).map((x, i) => Array(i + 1).fill(1))
Take a look at instances to validate our resolution
describe('fundamental assessments', () => {
it("Testing for 0", () => assert.deepEqual(pyramid(0), []));
it("Testing for 1", () => assert.deepEqual(pyramid(1), [[1]]));
it("Testing for two", () => assert.deepEqual(pyramid(2), [[1], [1, 1]]));
it("Testing for 3", () => assert.deepEqual(pyramid(3), [[1], [1, 1], [1, 1, 1]]));
});