-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution0092.js
47 lines (31 loc) · 1.06 KB
/
solution0092.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
--------------- 8 Kyu - Calculate average ------------------
Instructions:
Write a function which calculates the average of the numbers in a given list.
Note: Empty arrays should return 0.
-------------
Sample Tests
const chai = require("chai");
const assert = chai.assert;
chai.config.truncateThreshold=0;
describe("Basic tests", () => {
it("Testing for fixed tests", () => {
assert.strictEqual(find_average([1,1,1]), 1);
assert.strictEqual(find_average([1,2,3]), 2);
assert.strictEqual(find_average([1,2,3,4]), 2.5);
});
});
--------------
PREP
Parameters: an array of numbers.
Return: the average of the given numbers. If param is an empty array, return 0.
Example: [1,2,3,4] => 2.5
Psuedo Code:
-use forEach() loop to iterate through numbers in array
-declare sum variable, use += to add them together.
-divide sum variable by array.length
use conditonal/ternary to return 0 if array is empty by checking if array.length === 0
*/
function find_average(array) {
return array.length === 0 ? 0 : array.reduce((a, b) => a + b) / array.length
}