- Joined
- Dec 7, 2022
- Messages
- 5
- Reaction score
- 0
Hello guys !
My assignment is to Implement the getSpeedStatistic function, that accepts the testResults array and returns statistics as an array with 3 numbers:
function getSpeedStatistic(testResults) {
let min = testResults[0];
let max = testResults[0];
let sum = 0;
let average = 0;
for(const number of testResults){
if(number > max){
max = number;
};
if(number < min){
min = number;
};
sum += number;
average += Math.round(sum / testResults.length);
};
return [min, max, average];
};
It works for the lowest and highest value but it doesnt return the right average, i cant seem to find a pattern for the results either, it seems like it gives me random numbers, i dont understand the issue.
For example:
It should return [0, 8, 3] when input is [5, 0, 8, 1] - but it returns [0,8,9];
Should return [1, 18, 4] when input is [1, 2, 2, 3, 3, 3, 3, 18] - but it returns [1,18,11].
Should return [1, 9.2, 5] when input is [4.5, 6.7, 9.2, 1] - but it returns [1,9.2,14]
Please help !
My assignment is to Implement the getSpeedStatistic function, that accepts the testResults array and returns statistics as an array with 3 numbers:
- the first one is the lowest value;
- the second is the highest value;
- the last one is the average value, rounded down
function getSpeedStatistic(testResults) {
let min = testResults[0];
let max = testResults[0];
let sum = 0;
let average = 0;
for(const number of testResults){
if(number > max){
max = number;
};
if(number < min){
min = number;
};
sum += number;
average += Math.round(sum / testResults.length);
};
return [min, max, average];
};
It works for the lowest and highest value but it doesnt return the right average, i cant seem to find a pattern for the results either, it seems like it gives me random numbers, i dont understand the issue.
For example:
It should return [0, 8, 3] when input is [5, 0, 8, 1] - but it returns [0,8,9];
Should return [1, 18, 4] when input is [1, 2, 2, 3, 3, 3, 3, 18] - but it returns [1,18,11].
Should return [1, 9.2, 5] when input is [4.5, 6.7, 9.2, 1] - but it returns [1,9.2,14]
Please help !