Nest all content into snippets

This commit is contained in:
Angelos Chalaris
2023-05-07 16:07:29 +03:00
parent 2ecadbada9
commit 6a45d2ec07
1240 changed files with 0 additions and 0 deletions

23
snippets/js/s/average.md Normal file
View File

@ -0,0 +1,23 @@
---
title: Average of numbers
type: snippet
language: javascript
tags: [math]
cover: interior-8
dateModified: 2020-10-22T20:23:47+03:00
---
Calculates the average of two or more numbers.
- Use `Array.prototype.reduce()` to add each value to an accumulator, initialized with a value of `0`.
- Divide the resulting array by its length.
```js
const average = (...nums) =>
nums.reduce((acc, val) => acc + val, 0) / nums.length;
```
```js
average(...[1, 2, 3]); // 2
average(1, 2, 3); // 2
```