#### Noob

Hello guys !

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
My function:

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]

#### WhiteCube

Hello guys !

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
My function:

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]

Calculate the average once, after the loop.

#### MeMate

Exactly. And use Math.floor instead of round.
Code:
``````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.floor(sum / testResults.length);

return [min, max, average];
};``````

JavaScript:
``````function fn(data){
const ar = data.sort( (a, b) => a - b ),
min = ar[0],
max = ar[ar.length - 1],
avr = Math.floor( ar.reduce( (a, b) => a + b, 0 ) / ar.length );
return [min, max, avr];
}

console.log( fn([5, 0, 8, 1]) ); // [0, 8, 3]
console.log( fn([1, 2, 2, 3, 3, 3, 3, 18]) ); // [1, 18, 4]
console.log( fn([4.5, 6.7, 9.2, 1]) ); // [1, 9.2, 5]``````

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.