Mathematics
Algorithms
Logic
Numbers
Data Types
Code a function that will return the exact average grade of an array of grades.
function average(grades) { if (grades && grades.length) { return grades.reduce((total, grade) => total + grade, 0) / grades.length } }
def average(grades)grades.sum.to_f / grades.size unless grades.empty?end- function average(grades) {
- if (grades && grades.length) {
- return grades.reduce((total, grade) => total + grade, 0) / grades.length
- }
- }
Test.assertEquals(average([1, 2, 3]), 2) Test.assertEquals(average([2, 3]), 2.5) Test.assertNotEquals(average([2, 3]), 2) Test.assertEquals(average([0,0,0]), 0) Test.assertEquals(average([0]), 0) Test.assertEquals(average([]), undefined)
Test.assert_equals(average([1, 2, 3]), 2)Test.assert_equals(average([2, 3]), 2.5)Test.assert_not_equals(average([2, 3]), 2)Test.assert_equals(average([0,0,0]), 0)Test.assert_equals(average([0]), 0)Test.assert_equals(average([]), nil)- Test.assertEquals(average([1, 2, 3]), 2)
- Test.assertEquals(average([2, 3]), 2.5)
- Test.assertNotEquals(average([2, 3]), 2)
- Test.assertEquals(average([0,0,0]), 0)
- Test.assertEquals(average([0]), 0)
- Test.assertEquals(average([]), undefined)