| 3 | 3 | // convenience to get you started writing code faster. |
| 4 | 4 | // |
| 5 | 5 | |
| 6 | export class GradeSchool { | |
| 6 | class GradeSchool { | |
| 7 | constructor() { | |
| 8 | this._roster = {}; | |
| 9 | } | |
| 10 | ||
| 7 | 11 | /** |
| 8 | 12 | * @return {string[]} |
| 9 | 13 | */ |
| 10 | 14 | roster() { |
| 11 | throw new Error('Remove this line and implement the function'); | |
| 15 | const result = []; | |
| 16 | ||
| 17 | // Edge Case: Empty roster | |
| 18 | if (Object.keys(this._roster).length === 0) { | |
| 19 | return result; | |
| 20 | } | |
| 21 | ||
| 22 | // Get all grades and sort them numerically | |
| 23 | const grades = Object.keys(this._roster) | |
| 24 | .map(grade => parseInt(grade)) | |
| 25 | .sort((a, b) => a - b); | |
| 26 | ||
| 27 | // For each grade, get sorted students and add to result | |
| 28 | for (const grade of grades) { | |
| 29 | const sortedStudents = [...this._roster[grade]].sort(); | |
| 30 | result.push(...sortedStudents); | |
| 31 | } | |
| 32 | ||
| 33 | return result; | |
| 12 | 34 | } |
| 13 | 35 | |
| 14 | 36 | /** |
| 17 | 39 | * @return {boolean} |
| 18 | 40 | */ |
| 19 | 41 | add(student, level) { |
| 20 | throw new Error('Remove this line and implement the function'); | |
| 42 | // Edge Case: Invalid grade (non-integer or negative) | |
| 43 | if (!Number.isInteger(level) || level < 0) { | |
| 44 | return false; | |
| 45 | } | |
| 46 | ||
| 47 | // Edge Case: Empty student name | |
| 48 | if (!student || typeof student !== 'string') { | |
| 49 | return false; | |
| 50 | } | |
| 51 | ||
| 52 | // Check if student already exists in any grade | |
| 53 | for (const grade in this._roster) { | |
| 54 | if (this._roster[grade].includes(student)) { | |
| 55 | // Edge Case: Student already exists in the roster | |
| 56 | return false; | |
| 57 | } | |
| 58 | } | |
| 59 | ||
| 60 | // Initialize grade array if it doesn't exist | |
| 61 | if (!this._roster[level]) { | |
| 62 | this._roster[level] = []; | |
| 63 | } | |
| 64 | ||
| 65 | this._roster[level].push(student); | |
| 66 | return true; | |
| 21 | 67 | } |
| 22 | 68 | |
| 23 | 69 | /** |
| 25 | 71 | * @return {string[]} |
| 26 | 72 | */ |
| 27 | 73 | grade(level) { |
| 28 | throw new Error('Remove this line and implement the function'); | |
| 74 | // Edge Case: Grade doesn't exist | |
| 75 | if (!this._roster[level]) { | |
| 76 | return []; | |
| 77 | } | |
| 78 | ||
| 79 | // Return sorted list of students in the grade | |
| 80 | return [...this._roster[level]].sort(); | |
| 29 | 81 | } |
| 30 | 82 | } |
| 83 | ||
| 84 | // Handled Edge Cases: Empty roster, invalid grade (non-integer or negative), empty student name, student already exists in the roster, grade doesn't exist | |
| 85 | ||
| 86 | export { GradeSchool }; |