math
Math utility object that provides various methods for numbers.
The math object is extended from the built-in Math object. You can continue to use whatever we normally use.
Usage
To use this module, you first need to import the math object:
import { math } from '@ponsetya/core'
Methods
math.average()
The average() method calculates the average of the given numbers.
math.average(...values: number[]): number
Parameters
- ...values:
number[]
- The numbers to calculate the average of.
- Returns:
number
- The average of the given numbers.
Examples
console.log(math.average(1, 2, 3, 4, 5)) // 3
console.log(math.average(-1, 0, 1)) // 0
math.even()
The even() method filters the even numbers from the given numbers.
math.even(...values: number[]): number[]
Parameters
- ...values:
number[]
- The numbers to filter even numbers from.
- Returns:
number[]
- An array of even numbers.
Examples
console.log(math.even(1, 2, 3, 4, 5, 6)) // [2, 4, 6]
console.log(math.even(7, 9, 11)) // []
math.factorial()
The factorial() method calculates the factorial of a positive integer.
math.factorial(x: number): number
Throws: An error if x is a negative number.
Parameters
- x:
number
- The positive integer to calculate the factorial of.
- Returns:
boolean
- The factorial of x.
Examples
console.log(math.factorial(0)) // 1
console.log(math.factorial(1)) // 1
console.log(math.factorial(5)) // 120
math.odd()
The odd() method filters the odd numbers from the given numbers.
math.odd(...values: number[]): number[]
Parameters
- ...values:
number[]
- The numbers to filter odd numbers from.
- Returns:
number[]
- An array of odd numbers.
Examples
console.log(math.odd(1, 2, 3, 4, 5, 6)) // [1, 3, 5]
console.log(math.odd(8, 10, 12)) // []
math.sum()
The sum() method calculates the sum of the given numbers.
math.sum(...values: number[]): number
Parameters
- ...values:
number[]
- The numbers to calculate the sum of.
- Returns:
number
- The sum of the given numbers.
Examples
console.log(math.sum(1, 2, 3, 4, 5)) // 15
console.log(math.sum(-1, 0, 1)) // 0