Skip to content

Commit bcece5c

Browse files
committed
Implement calculateMean function with input validation and calculation logic
1 parent d42b02b commit bcece5c

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

‎Sprint-1/implement/mean.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
function calculateMean(list) {}
1+
function calculateMean(list) {
2+
if (list === undefined) {
3+
throw new Error("calculateMean requires an array of numbers");
4+
}
5+
if (!Array.isArray(list)) {
6+
throw new Error("calculateMean requires an array of numbers");
7+
}
8+
if (list.length === 0) {
9+
throw new Error("calculateMean requires a non-empty array");
10+
}
11+
let total = 0;
12+
for (const elements of list) {
13+
if (typeof elements !== "number") {
14+
throw new Error("calculateMean requires an array of numbers");
15+
}
16+
total = total + elements;
17+
}
18+
return total / list.length;
19+
}
220

321
module.exports = calculateMean;

0 commit comments

Comments
 (0)