Update weightedAverage (#1251)
Co-authored-by: Angelos Chalaris <chalarangelo@gmail.com>
This commit is contained in:
17
snippets/weightedAverage.md
Normal file
17
snippets/weightedAverage.md
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
---
|
||||||
|
title: weightedAverage
|
||||||
|
tags: math,array,beginner
|
||||||
|
---
|
||||||
|
|
||||||
|
Returns the weighted average of two or more numbers.
|
||||||
|
|
||||||
|
- Use `Array.prototype.reduce()` to add each value to an accumulator, initialized with a value of `0` and divide by the `length` of the array.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const weightedAverage = (nums, weights) =>
|
||||||
|
nums.reduce((acc, n, i) => acc + (weights[i] * n), 0) / nums.length;
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
weightedAverage([1, 2, 3], [0.6,0.2,0.3]); // 0.6333
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user