Skip to content

Commit 10febce

Browse files
authored
style: cleanup PascalTriangle (TheAlgorithms#1606)
* tests: add missing tests for `PascalTriangle` * style: remove redundant branch in `generate` * tests: simplify tests of `PascalTriangle`
1 parent f313498 commit 10febce

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

Maths/PascalTriangle.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ const generate = (numRows) => {
1717
return []
1818
} else if (numRows === 1) {
1919
return [[1]]
20-
} else if (numRows === 2) {
21-
return [[1], [1, 1]]
2220
} else {
2321
for (let i = 2; i < numRows; i++) {
2422
addRow(triangle)

Maths/test/PascalTriangle.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1+
import { expect } from 'vitest'
12
import { generate } from '../PascalTriangle'
23

34
describe('Pascals Triangle', () => {
4-
it('should have the the same length as the number', () => {
5-
const pascalsTriangle = generate(5)
6-
expect(pascalsTriangle.length).toEqual(5)
7-
})
8-
it('should have same length as its index in the array', () => {
9-
const pascalsTriangle = generate(5)
5+
it.each([
6+
[0, []],
7+
[1, [[1]]],
8+
[2, [[1], [1, 1]]],
9+
[3, [[1], [1, 1], [1, 2, 1]]],
10+
[4, [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]]],
11+
[5, [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]], [1, 4, 6, 4, 1]]
12+
])('check with %j', (input, expected) => {
13+
const pascalsTriangle = generate(input)
14+
expect(pascalsTriangle.length).toEqual(input)
1015
pascalsTriangle.forEach((arr, index) => {
1116
expect(arr.length).toEqual(index + 1)
1217
})
13-
})
14-
it('should return an array of arrays', () => {
15-
const pascalsTriangle = generate(3)
16-
expect(pascalsTriangle).toEqual(
17-
expect.arrayContaining([[1], [1, 1], [1, 2, 1]])
18-
)
18+
expect(pascalsTriangle).toEqual(expect.arrayContaining(expected))
1919
})
2020
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy