jeudi 27 août 2020

Math.floor not working for cartian test case

I am doing a coding challenge that requires me to find the rounded down average of integer array elements. The challenge has 3 test cases, 2 of which pass, except for the last one which does not and i have no idea why. This is my code:

function getAverage(marks){
  //TODO : calculate the downwar rounded average of the marks array
   return Math.floor(marks.reduce((acc, cur) => {
    return acc + cur;
  })) / marks.length;
  
}

And these are the test cases:

Test.assertEquals(getAverage([2,2,2,2]),2);
Test.assertEquals(getAverage([1,2,3,4,5,]),3);
Test.assertEquals(getAverage([1,1,1,1,1,1,1,2]),1);

The last test case returns 1.125 instead of 1, even though I am rounding down the result. What could be the reason for this?

Aucun commentaire:

Enregistrer un commentaire