I tried the module pattern for the first time, with this exercise as a problem to solve: http://exercism.io/exercises/javascript/grade-school/readme

It's a small exercise requiring simple tasks :

  • Add a student's name to the roster for a grade
    • "Add Jim to grade 2."
    • "OK."
  • Get a list of all students enrolled in a grade
    • "Which students are in grade 2?"
    • "We've only got Jim just now."
  • Get a sorted list of all students in all grades. Grades should sort as 1, 2, 3, etc., and students within a grade should be sorted alphabetically by name.
    • "Who all is enrolled in school right now?"
    • "Grade 1: Anna, Barb, and Charlie. Grade 2: Alex, Peter, and Zoe. Grade 3…"

This is the test suite (jasmine based) http://exercism.io/exercises/javascript/grade-school

This is my implementation with the revealing module pattern

function School () {
  var rosterDb = {};

  // Private
  var sort = function(gradeNumber) {
    return rosterDb[gradeNumber].sort();

  // Public
  var roster = function() {
    for(var index in rosterDb) { 
    return rosterDb;

  var add = function(student, grade) {
    if (rosterDb[grade] == undefined) rosterDb[grade] = [];

  var grade = function(grade) {
    if (rosterDb[grade] != undefined) return sort(grade);
    else return rosterDb[grade] = [];

  return {
    roster: roster,
    add: add,
    grade: grade

module.exports = School;

I just had two objectives : 1/ make it pass all tests 2/ use the revealing module pattern. The first part is easy to verify, but the second one is harder. I would like some opinions on that


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.