Skip to content

Commit 57025ac

Browse files
committed
Added solution and fix tests
1 parent 3be5e72 commit 57025ac

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// your code here
2+
function getAverageOfElementsAtProperty(obj, key){
3+
if(!obj[key] || !Array.isArray(obj[key]) || obj[key].length == 0){
4+
return 0;
5+
}
6+
7+
let sum = 0;
8+
9+
for (let i = 0; i < obj[key].length; i++) {
10+
sum+=obj[key][i]
11+
}
12+
13+
return sum / obj[key].length;
14+
}

exercises/100-getAverageOfElementsAtProperty/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ test('If the property at the given key is not an array, it should return 0', ()
3939

4040
test('If there is no property at the given key, it should return 0', () => {
4141
let obj = {};
42-
let output = getAverageOfElementsAtProperty(obj['key']);
42+
let output = getAverageOfElementsAtProperty(obj, "key");
4343
expect(output).toBe(0);
4444
});
4545

0 commit comments

Comments
 (0)