style: cleanup PascalTriangle (#1606)

* tests: add missing tests for `PascalTriangle`

* style: remove redundant branch in `generate`

* tests: simplify tests of `PascalTriangle`
This commit is contained in:
Piotr Idzik
2024-02-09 18:01:20 +01:00
committed by GitHub
parent f31349812c
commit 10febce31a
2 changed files with 12 additions and 14 deletions

View File

@ -17,8 +17,6 @@ const generate = (numRows) => {
return []
} else if (numRows === 1) {
return [[1]]
} else if (numRows === 2) {
return [[1], [1, 1]]
} else {
for (let i = 2; i < numRows; i++) {
addRow(triangle)

View File

@ -1,20 +1,20 @@
import { expect } from 'vitest'
import { generate } from '../PascalTriangle'
describe('Pascals Triangle', () => {
it('should have the the same length as the number', () => {
const pascalsTriangle = generate(5)
expect(pascalsTriangle.length).toEqual(5)
})
it('should have same length as its index in the array', () => {
const pascalsTriangle = generate(5)
it.each([
[0, []],
[1, [[1]]],
[2, [[1], [1, 1]]],
[3, [[1], [1, 1], [1, 2, 1]]],
[4, [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]]],
[5, [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]], [1, 4, 6, 4, 1]]
])('check with %j', (input, expected) => {
const pascalsTriangle = generate(input)
expect(pascalsTriangle.length).toEqual(input)
pascalsTriangle.forEach((arr, index) => {
expect(arr.length).toEqual(index + 1)
})
})
it('should return an array of arrays', () => {
const pascalsTriangle = generate(3)
expect(pascalsTriangle).toEqual(
expect.arrayContaining([[1], [1, 1], [1, 2, 1]])
)
expect(pascalsTriangle).toEqual(expect.arrayContaining(expected))
})
})