Skip to content

Commit

Permalink
Improved condition
Browse files Browse the repository at this point in the history
  • Loading branch information
theoplawinski committed Nov 21, 2023
1 parent 357a873 commit c2060b8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/array/getClosestNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function getClosestNumberInArray(
input: number,
array: number[],
): number {
if (input === null || input === undefined || array.length < 1) return 0
if (input == null || array?.length < 1) return

return array.reduce((a, b) =>
Math.abs(b - input) < Math.abs(a - input) ? b : a,
Expand Down
6 changes: 3 additions & 3 deletions tests/array/closestNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ describe("closestNumberInArray", () => {


const data: [number, number[], number][] = [
[-100, [], 0],
[null, [10, 20], 0],
[undefined, [], 0],
[-100, [], undefined],
[null, [10, 20], undefined],
[undefined, [], undefined],

[-1, [-100, 0, 0.25, 0.5, 0.75, 1], 0],
[-51, [-100, 0, 0.25, 0.5, 0.75, 1], -100],
Expand Down

0 comments on commit c2060b8

Please sign in to comment.