Skip to content

Instantly share code, notes, and snippets.

@luchiago
Last active January 22, 2020 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luchiago/c518e13dd4edbad30eec77b62e0644e7 to your computer and use it in GitHub Desktop.
Save luchiago/c518e13dd4edbad30eec77b62e0644e7 to your computer and use it in GitHub Desktop.
An example using reduce and map functions from JavaScript
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