Skip to content

Instantly share code, notes, and snippets.

@heymichaelp
Last active August 29, 2015 14:02
Show Gist options
  • Save heymichaelp/69c384e5d3f622cc6d38 to your computer and use it in GitHub Desktop.
Save heymichaelp/69c384e5d3f622cc6d38 to your computer and use it in GitHub Desktop.
Value Objects: Implementation
var _ = require('underscore');
var Grade = function(percentage) {
this.percentage = percentage;
this.grade = this.grade(percentage);
};
Grade.prototype = _.extend(Grade.prototype, {
passingGradeLetters: function() {
return _.chain(Grade.grades).where({passing: true}).pluck('letter').value();
},
grade: function(percentage) {
return _.find(Grade.grades, function(grade) {
return percentage >= grade.minimumPercentage;
});
},
letterGrade: function() {
return this.grade.letter;
},
isPassing: function() {
return this.grade.passing
},
isImprovementFrom: function(grade) {
return this.isBetterThan(grade);
},
isBetterThan: function(grade) {
return this.percentage > grade.percentage;
},
valueOf: function() {
return this.percentage;
}
});
Grade.grades = [
{letter: 'A', minimumPercentage: 0.9, passing: true},
{letter: 'B', minimumPercentage: 0.8, passing: true},
{letter: 'C', minimumPercentage: 0.7, passing: true},
{letter: 'D', minimumPercentage: 0.6, passing: true},
{letter: 'F', minimumPercentage: 0, passing: false}
];
module.exports = Grade;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment