Skip to content

Commit

Permalink
fix: fix min
Browse files Browse the repository at this point in the history
  • Loading branch information
supperchong committed Mar 13, 2022
1 parent f4d3acc commit 9bd33f9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ export function min(...arg: number[] | number[][]): number {
return Math.min(...arg)
}

let min = -Infinity
let min = Infinity

for (const num of arg) {
min = Math.min(min, num)
Expand Down
12 changes: 12 additions & 0 deletions test/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,25 @@ describe('test max', () => {
it('should return the max value', () => {
assert.strictEqual(max(1, 3, 2), 3)
})
it('should return the max value when length is too large', () => {
let nums = Array(200000).fill(2)
nums[0] = 1
assert.throws(() => Math.max(...nums), RangeError)
assert.strictEqual(max(nums), 2)
})
})

describe('test min', () => {
it('should return the min value', () => {
assert.strictEqual(min(1, 3, 2), 1)
assert.strictEqual(min([1, 3, 2]), 1)
})
it('should return the min value when length is too large', () => {
let nums = Array(200000).fill(2)
nums[0] = 1
assert.throws(() => Math.min(...nums), RangeError)
assert.strictEqual(min(nums), 1)
})
})

describe('test unique', () => {
Expand Down

0 comments on commit 9bd33f9

Please sign in to comment.