https://github.com/lamansky/nearest-number
From a list of numbers, picks the one closest to a given target. Packaged as a Node.js module.
https://github.com/lamansky/nearest-number
Last synced: 3 months ago
JSON representation
From a list of numbers, picks the one closest to a given target. Packaged as a Node.js module.
- Host: GitHub
- URL: https://github.com/lamansky/nearest-number
- Owner: lamansky
- License: mit
- Created: 2017-11-27T09:32:16.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-27T10:08:54.000Z (over 8 years ago)
- Last Synced: 2025-03-01T17:49:04.891Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 1.95 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
# nearest-number
From a list of numbers, picks the one closest to a given target. Packaged as a Node.js module.
## Installation
```bash
npm install nearest-number --save
```
## Usage
```javascript
const nearestNumber = require('nearest-number')
// Out of the numbers in the list, the number nearest to 13 is 10
nearestNumber([10, 25, 30], 13) // 10
// Returns the index of 10 in the array
nearestNumber([10, 25, 30], 13, {returnIndex: true}) // 0
```