Skip to content

Instantly share code, notes, and snippets.

@heymichaelp
Last active April 11, 2016 07:44
Show Gist options
  • Save heymichaelp/1a2bcc38c6e3cc603ccb to your computer and use it in GitHub Desktop.
Save heymichaelp/1a2bcc38c6e3cc603ccb to your computer and use it in GitHub Desktop.
Service Objects: Implementation
var _ = require('underscore');
/**
* For a given student, determine whether or not they are passing
* @constructor
* @param {Object} student - The student that being evaluated.
*/
var DetermineStudentPassingStatus = function(student) {
this.student = student;
}
DetermineStudentPassingStatus.minimumPassingPercentage = 0.6
DetermineStudentPassingStatus.prototype = _.extend(DetermineStudentPassingStatus.prototype, {
/**
* The API method for calculating passing status from
* a set of assignments that belong to the student
* @param {Array} assignments - The assignments to be used in calculating passing status.
*/
fromAssignments: function(assignments) {
return _.compose(
this.determinePassingStatus,
this.averageAssignmentGrade,
this.extractAssignmentGrades
).call(this, assignments);
},
// return the 'grade' value objects from each assignment
extractAssignmentGrades: function(assignments) {
return _.pluck(assignments, 'grade');
},
// take all grades and find the average percentage
averageAssignmentGrade: function(grades) {
return grades.reduce(function(memo, grade) {
return memo + grade.percentage;
}, 0) / grades.length;
},
// compare the averages from all assignments to the minimumPassingPercentage value defined above
determinePassingStatus: function(averageGrade) {
return averageGrade >= DetermineStudentPassingStatus.minimumPassingPercentage;
}
});
module.exports = DetermineStudentPassingStatus;
// new DetermineStudentPassingStatus(student).fromAssignments([assignments]);
// => true/false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment