Trying to get the average value of the elements, please help ! JavaScript

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:

  • 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]

Please help !
 
Joined
Sep 21, 2022
Messages
126
Reaction score
16
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]

Please help !
Calculate the average once, after the loop.
 
Joined
Mar 11, 2022
Messages
227
Reaction score
32
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];
};
 
Joined
Jul 3, 2022
Messages
93
Reaction score
23
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]
 

Ask a Question

Want to reply to this thread or ask your own question?

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,808
Messages
2,569,686
Members
45,452
Latest member
AmberLayde

Latest Threads

Top