Skip to content

Instantly share code, notes, and snippets.

@iam-peekay
Last active April 16, 2019 16:01
Show Gist options
  • Save iam-peekay/3d29068bd0097c53af41 to your computer and use it in GitHub Desktop.
Save iam-peekay/3d29068bd0097c53af41 to your computer and use it in GitHub Desktop.
var myGradesCalculate = (function () {
// Keep this variable private inside this closure scope
var myGrades = [93, 95, 88, 0, 55, 91];
var average = function() {
var total = myGrades.reduce(function(accumulator, item) {
return accumulator + item;
}, 0);
return'Your average grade is ' + total / myGrades.length + '.';
};
var failing = function() {
var failingGrades = myGrades.filter(function(item) {
return item < 70;
});
return 'You failed ' + failingGrades.length + ' times.';
};
// Explicitly reveal public pointers to the private functions
// that we want to reveal publicly
return {
average: average,
failing: failing
}
})();
myGradesCalculate.failing(); // 'You failed 2 times.'
myGradesCalculate.average(); // 'Your average grade is 70.33333333333333.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment