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

rootulp's solution

to Grade School in the TypeScript Track

Published at May 18 2021 · 0 comments
Test suite

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 TypeScript to install the necessary dependencies:



Install assignment dependencies:

$ yarn install

Making the test suite pass

Execute the tests with:

$ yarn test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable 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.


import GradeSchool from './grade-school'

describe('School', () => {
  let gradeSchool: GradeSchool

  beforeEach(() => {
    gradeSchool = new GradeSchool()

  it('a new school has an empty roster', () => {
    expect(gradeSchool.studentRoster().size === 0).toEqual(true)

  xit("A student can't be in two different grades", () => {
    gradeSchool.addStudent('Aimee', 2)
    gradeSchool.addStudent('Aimee', 1)

    const emptyGrade = gradeSchool.studentsInGrade(2)

  xit('adding a student adds them to the roster for the given grade', () => {
    gradeSchool.addStudent('Aimee', 2)

    const expectedDb = new Map(Object.entries({ 2: ['Aimee'] }))

  xit('adding more students to the same grade adds them to the roster', () => {
    gradeSchool.addStudent('Blair', 2)
    gradeSchool.addStudent('James', 2)
    gradeSchool.addStudent('Paul', 2)

    const expectedDb = new Map(
      Object.entries({ 2: ['Blair', 'James', 'Paul'] })

  xit('adding students to different grades adds them to the roster', () => {
    gradeSchool.addStudent('Chelsea', 3)
    gradeSchool.addStudent('Logan', 7)

    const expectedDb = new Map(Object.entries({ 3: ['Chelsea'], 7: ['Logan'] }))

  xit('grade returns the students in that grade in alphabetical order', () => {
    gradeSchool.addStudent('Franklin', 5)
    gradeSchool.addStudent('Bradley', 5)
    gradeSchool.addStudent('Jeff', 1)

    const expectedStudents = ['Bradley', 'Franklin']

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

  xit('the students names in each grade in the roster are sorted', () => {
    gradeSchool.addStudent('Jennifer', 4)
    gradeSchool.addStudent('Kareem', 6)
    gradeSchool.addStudent('Christopher', 4)
    gradeSchool.addStudent('Kyle', 3)

    const expectedSortedStudents = new Map(
        3: ['Kyle'],
        4: ['Christopher', 'Jennifer'],
        6: ['Kareem'],

  xit('roster cannot be modified outside of module', () => {
    gradeSchool.addStudent('Aimee', 2)
    const roster = gradeSchool.studentRoster()
    const result = roster.get('2') || []
    const expectedDb = new Map(Object.entries({ 2: ['Aimee'] }))

  xit('roster cannot be modified outside of module using studentsInGrade()', () => {
    gradeSchool.addStudent('Aimee', 2)
    const expectedDb = new Map(Object.entries({ 2: ['Aimee'] }))
export default class GradeSchool {

    private roster: Map<number, string[]>

    constructor() {
        this.roster = new Map<number, string[]>()

    public studentRoster(): ReadonlyMap<string, string[]> {
        const result = new Map<string, string[]>();
        this.roster.forEach((students, grade) => {
            result.set(grade.toString(), [...students])
        return result;

    public addStudent(name: string, grade: number): void {
        const students = this.studentsInGrade(grade);
        const newStudents = [...students, name].sort()
        this.roster.set(grade, newStudents)

    public studentsInGrade(grade: number): string[] {
        const students = this.roster.get(grade)
        if (students) {
            return [...students]
        return []

    private removeStudentIfAlreadyExists(name: string) {
        let newRoster = new Map<number, string[]>();

        this.roster.forEach((students, grade) => {
            const newStudents = students.filter(student => student !== name)
            newRoster.set(grade, newStudents);

        this.roster = newRoster;


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?