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 [] return []
} else if (numRows === 1) { } else if (numRows === 1) {
return [[1]] return [[1]]
} else if (numRows === 2) {
return [[1], [1, 1]]
} else { } else {
for (let i = 2; i < numRows; i++) { for (let i = 2; i < numRows; i++) {
addRow(triangle) addRow(triangle)

View File

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