-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
891e0e0
commit f1ccc1d
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
|
||
|
||
## product variable | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
Multiplies together all the elements of a list. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
product: { | ||
(val: number[]): number; | ||
(val: bigint[]): bigint; | ||
} | ||
``` | ||
## Example | ||
```ts | ||
product([1, 2, 3, 4, 5]) // 120 | ||
product([1n, 2n, 3n, 4n, 5n]) //120n | ||
product([]) // 0 | ||
|
||
``` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { first } from '@/first' | ||
import { isNumber } from '@/isNumber' | ||
import { isUndefined } from '@/isUndefined' | ||
import { multiply } from '@/multiply' | ||
|
||
/** | ||
* Multiplies together all the elements of a list. | ||
* | ||
* @param val - list An array of numbers | ||
* @returns The product of all the numbers in the list | ||
* | ||
* @example | ||
* ```ts | ||
* product([1, 2, 3, 4, 5]) // 120 | ||
* product([1n, 2n, 3n, 4n, 5n]) //120n | ||
* product([]) // 0 | ||
* ``` | ||
* | ||
* @beta | ||
*/ | ||
const product: { | ||
(val: number[]): number | ||
(val: bigint[]): bigint | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} = (val: any) => { | ||
const head = first(val) as number | bigint | ||
if (isUndefined(head)) return 0 | ||
const init = isNumber(head) ? 1 : 1n | ||
|
||
return val.reduce(multiply, init) | ||
} | ||
|
||
export { product } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { product } from '@/product' | ||
|
||
describe('product', () => { | ||
const tableNumber: [number[], number][] = [ | ||
[[], 0], | ||
[[0, 0], 0], | ||
[[1], 1], | ||
[[1, 2, 3, 4, 5], 120], | ||
[[1, -2, 3, -4, 5], 120] | ||
] | ||
|
||
it.each(tableNumber)('product(%s) -> %s', (val, expected) => { | ||
expect(product(val)).toBe(expected) | ||
}) | ||
const tableBigint: [bigint[], bigint][] = [ | ||
[[0n], 0n], | ||
[[1n], 1n], | ||
[[1n, 2n, 3n, 4n, 5n], 120n], | ||
[[1n, -2n, 3n, -4n, 5n], 120n] | ||
] | ||
|
||
it.each(tableBigint)('product(%s) -> %s', (val, expected) => { | ||
expect(product(val)).toBe(expected) | ||
}) | ||
}) |