Skip to content

Instantly share code, notes, and snippets.

@furqanbaqai
Last active May 21, 2020 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save furqanbaqai/3214c4c059800944f31264ab1949dee8 to your computer and use it in GitHub Desktop.
Save furqanbaqai/3214c4c059800944f31264ab1949dee8 to your computer and use it in GitHub Desktop.
types, maps, iterators
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-4.html
// https://exercism.io/tracks/typescript/exercises/grade-school/solutions/0798baf5852e4b6e95220a1cbf894eb0
// https://www.digitalocean.com/community/tutorials/typescript-type-alias
type RosterType<T> = Map<T, string[]>;
class GradeSchool{
private roster: RosterType<number> = new Map();
public studentRoster(): RosterType<string> {
const strRoster: RosterType<string> = new Map();
for(const key of this.roster.keys()){
strRoster.set(key.toString(), this.studentsInGrade(key));
}
return strRoster;
}
public addStudent(name: string, grade: number){
let existingStudents = (this.roster.get(grade) || []).concat(name);
this.roster.set(grade, existingStudents);
}
public studentsInGrade(grade: number): any{
return (this.roster.get(grade) || []).sort().slice();
}
}
export default GradeSchool;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment