Nest all content into snippets
This commit is contained in:
23
snippets/js/s/closest.md
Normal file
23
snippets/js/s/closest.md
Normal file
@ -0,0 +1,23 @@
|
||||
---
|
||||
title: Closest numeric match
|
||||
type: snippet
|
||||
language: javascript
|
||||
tags: [math]
|
||||
author: chalarangelo
|
||||
cover: sparkles
|
||||
dateModified: 2022-03-30T05:00:00-04:00
|
||||
---
|
||||
|
||||
Finds the closest number from an array.
|
||||
|
||||
- Use `Array.prototype.reduce()` to scan all elements of the array.
|
||||
- Use `Math.abs()` to compare each element's distance from the target value, storing the closest match.
|
||||
|
||||
```js
|
||||
const closest = (arr, n) =>
|
||||
arr.reduce((acc, num) => (Math.abs(num - n) < Math.abs(acc - n) ? num : acc));
|
||||
```
|
||||
|
||||
```js
|
||||
closest([6, 1, 3, 7, 9], 5); // 6
|
||||
```
|
||||
Reference in New Issue
Block a user