-
-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy path3.js
More file actions
23 lines (21 loc) · 531 Bytes
/
3.js
File metadata and controls
23 lines (21 loc) · 531 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
There is a bug in this code which may not be immediately obvious.
Hint: Try different test cases to uncover the bug. Then update the code to
resolve the bug.
*/
function calculateAverage(arr) {
if (arr.length === 0 ) {
return 0;
}
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum / arr.length;
}
let arr = [];
console.log(calculateAverage(arr));
arr = [8];
console.log(calculateAverage(arr));
arr = [10,5,1];
console.log(calculateAverage(arr));