Skip to content

Commit 1510060

Browse files
committed
Bonus - Iteration ironhack-labs#4.1: A generic avg() function COMPLETE
1 parent e557792 commit 1510060

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/functions-and-arrays.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,26 @@ function averageWordLength(words) {
8686

8787
//Bonus
8888

89+
function avg(arr) {
90+
if (arr.length<=0) {
91+
return null
92+
}
93+
let result = 0;
94+
for (let i = 0; i < arr.length; i++) {
95+
96+
if( typeof arr[i] == 'boolean') {
97+
result += arr[i]
98+
} else if( typeof arr[i] == 'number') {
99+
result += arr[i]
100+
} else if( typeof arr[i] == 'string') {
101+
result += arr[i].length
102+
} else {
103+
throw new Error("Unsupported data type sir or ma'am")
104+
}
105+
}
106+
return Math.floor((result / arr.length) * 100) /100
107+
}
108+
89109
// Iteration #5: Unique arrays
90110
const wordsUnique = [
91111
'crab',

0 commit comments

Comments
 (0)