Start of main content## JavaScript Math Snippets

The JavaScript snippet collection contains a wide variety of ES6 helper functions. It includes helpers for dealing with primitives, arrays and objects, as well as algorithms, DOM manipulation functions and Node.js utilities.

### Array ranking

JavaScript, Array

Calculates the ranking of an array based on a comparator function.

### Mapped array sum

JavaScript, Math

Calculates the sum of an array, after mapping each element to a value using the provided function.

### Partial sum array

JavaScript, Math

Creates an array of partial sums.

### Reverse number

JavaScript, Math

Reverses a number.

### Geometric progression

JavaScript, Math

Initializes an array containing the numbers in the specified geometric progression range.

### Integer to roman numeral

JavaScript, Math

Converts an integer to its roman numeral representation. Accepts value between

`1`

and`3999`

(both inclusive).### Max array value based on function

JavaScript, Math

Returns the maximum value of an array, after mapping each element to a value using the provided function.

### N max elements

JavaScript, Array

Returns the

`n`

maximum elements from the provided array.### Min array value based on function

JavaScript, Math

Returns the minimum value of an array, after mapping each element to a value using the provided function.

### N min elements

JavaScript, Array

Returns the

`n`

minimum elements from the provided array.### Pretty-print number of bytes

JavaScript, String

Converts a number in bytes to a human-readable string.

### Insertion index in sorted array based on function

JavaScript, Array

Finds the lowest index at which a value should be inserted into an array in order to maintain its sorting order, based on the provided iterator function.

### How can I remove trailing zeros from a number in JavaScript?

JavaScript, Math

When formatting decimal values in JavaScript, trailing zeros can be undesired. Here's how to deal with them.

### Cross product of arrays

JavaScript, Array

Creates a new array out of the two supplied by creating each possible pair from the arrays.

### Fibonacci

JavaScript, Math

Generates an array, containing the Fibonacci sequence, up until the nth term.