Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of rootulp

rootulp's solution

to Grade School in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given students' names along with the grade that they are in, create a roster for the school.

In the end, you should be able to:

  • 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…"

Note that all our students only have one name. (It's a small town, what do you want?)

For bonus points

Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:

  • If you're working in a language with mutable data structures and your implementation allows outside code to mutate the school's internal DB directly, see if you can prevent this. Feel free to introduce additional tests.

Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?


Go through the setup instructions for JavaScript to install the necessary dependencies:


Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine grade-school.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.


A pairing session with Phil Battos at gSchool http://gschool.it

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


var School = require('./grade-school');

describe('School', function () {
  var school;

  beforeEach(function () {
    school = new School();

  it('a new school has an empty roster', function () {

  xit('adding a student adds them to the roster for the given grade', function () {
    school.add('Aimee', 2);
    var expectedDb = { 2: [ 'Aimee' ] };

  xit('adding more students to the same grade adds them to the roster', function () {
    school.add('Blair', 2);
    school.add('James', 2);
    school.add('Paul', 2);
    var expectedDb = { 2: [ 'Blair', 'James', 'Paul' ] };

  xit('adding students to different grades adds them to the roster', function () {
    school.add('Chelsea', 3);
    school.add('Logan', 7);
    var expectedDb = { 3: [ 'Chelsea' ], 7: [ 'Logan' ] };

  xit('grade returns the students in that grade in alphabetical order', function () {
    school.add('Franklin', 5);
    school.add('Bradley', 5);
    school.add('Jeff', 1);
    var expectedStudents = [ 'Bradley', 'Franklin' ];

  xit('grade returns an empty array if there are no students in that grade', function () {

  xit('the students names in each grade in the roster are sorted', function () {
    school.add('Jennifer', 4);
    school.add('Kareem', 6);
    school.add('Christopher', 4);
    school.add('Kyle', 3);
    var sorted = {
      3: [ 'Kyle' ],
      4: [ 'Christopher', 'Jennifer' ],
      6: [ 'Kareem' ]
var School = function() {
  'use strict';

  var roster = {};

  function getRoster() {
    return roster;

  function setRoster(name, grade) {
    if (roster[grade]) {
    } else {
      roster[grade] = [name];

  function grade(grade) {
    if (roster[grade]) {
      return roster[grade];
    } else {
      return [];

  return {
    roster: getRoster,
    add: setRoster,
    grade: grade

module.exports = School

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?