Last active
January 6, 2017 17:27
-
-
Save newmanbrad/1f380fe9015edebf1338adeeacbd41c2 to your computer and use it in GitHub Desktop.
Example of declarative style
This file contains hidden or 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
/** | |
Decalartive style example of a program that will find the average grade for classes with | |
more than one student enrolled. | |
**/ | |
let courses = [ | |
{ name: 'functional fundamentals', enrolled: 2, grade: 100, students: 30 }, | |
{ name: 'category theory', enrolled: 2, grade: 80, students: 25 }, | |
{ name: 'basket weaving', enrolled: 1, grade: 89, students: 43 } | |
]; | |
function average(numbers) { | |
const total = numbers.reduce((a,b) => a + b, 0); | |
return (numbers.length == 0) ? 0 : total / numbers.length; | |
} | |
function pluck (objects, key) { | |
return objects.map(object => object[key]); | |
} | |
let overOneEnrolled = courses.filter(course => course.enrolled > 1); | |
let averageGrade = average(pluck(overOneEnrolled, 'grade')); | |
console.info('The average grade for courses with more than one person enrolled: ', averageGrade); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment