Last active
January 22, 2020 01:31
-
-
Save luchiago/c518e13dd4edbad30eec77b62e0644e7 to your computer and use it in GitHub Desktop.
An example using reduce and map functions from JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const students_grades = [ | |
[7.0, 8.0, 6.0], | |
[10.0, 10.0, 10.0], | |
[5.0, 9.0, 10.0] | |
] | |
const reducer = (exams) => { | |
let starter = 0 | |
let number_of_exams = 3 | |
result = exams.reduce( | |
somatory, starter | |
); | |
return result / number_of_exams; | |
} | |
const somatory = (accumulator, grade) => { | |
return accumulator + grade; | |
} | |
average = students_grades.map(reducer); | |
console.log(average); | |
// Output [7, 10, 8] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment