| 1 | { | |
| 1 | 2 | // |
| 2 | 3 | // This is only a SKELETON file for the 'Kindergarten Garden' exercise. |
| 3 | 4 | // It's been provided as a convenience to get you started writing code faster. |
| 25 | 26 | C: 'clover', |
| 26 | 27 | }; |
| 27 | 28 | |
| 28 | export class Garden { | |
| 29 | class Garden { | |
| 29 | 30 | /** |
| 30 | 31 | * @param {string} diagram |
| 31 | 32 | * @param {string[]} students |
| 32 | 33 | */ |
| 33 | 34 | constructor(diagram, students = DEFAULT_STUDENTS) { |
| 34 | throw new Error('Remove this line and implement the function'); | |
| 35 | // Edge Case: Handle empty diagram | |
| 36 | if (!diagram) { | |
| 37 | this.rows = ['', '']; | |
| 38 | } else { | |
| 39 | this.rows = diagram.split('\n'); | |
| 40 | // Edge Case: Ensure we always have two rows | |
| 41 | if (this.rows.length === 1) { | |
| 42 | this.rows.push(''); | |
| 43 | } else if (this.rows.length === 0) { | |
| 44 | this.rows = ['', '']; | |
| 45 | } | |
| 46 | } | |
| 47 | ||
| 48 | // Edge Case: Handle empty or null students array | |
| 49 | if (!students || students.length === 0) { | |
| 50 | this.students = [...DEFAULT_STUDENTS]; | |
| 51 | } else { | |
| 52 | this.students = [...students].sort(); | |
| 53 | } | |
| 35 | 54 | } |
| 36 | 55 | |
| 37 | 56 | /** |
| 39 | 58 | * @returns {string[]} |
| 40 | 59 | */ |
| 41 | 60 | plants(student) { |
| 42 | throw new Error('Remove this line and implement the function'); | |
| 61 | // Edge Case: Student not found in the list | |
| 62 | const studentIndex = this.students.indexOf(student); | |
| 63 | if (studentIndex === -1) { | |
| 64 | return []; | |
| 65 | } | |
| 66 | ||
| 67 | const startPosition = studentIndex * 2; | |
| 68 | const row1 = this.rows[0] || ''; | |
| 69 | const row2 = this.rows[1] || ''; | |
| 70 | ||
| 71 | // Edge Case: Handle when rows are too short for the student's position | |
| 72 | if (startPosition >= row1.length || startPosition >= row2.length) { | |
| 73 | return []; | |
| 74 | } | |
| 75 | ||
| 76 | const plantCodes = [ | |
| 77 | row1[startPosition], | |
| 78 | row1[startPosition + 1], | |
| 79 | row2[startPosition], | |
| 80 | row2[startPosition + 1] | |
| 81 | ]; | |
| 82 | ||
| 83 | // Edge Case: Handle invalid plant codes | |
| 84 | return plantCodes.map(code => { | |
| 85 | return PLANT_CODES[code] || 'grass'; // Default to grass for unknown codes | |
| 86 | }); | |
| 43 | 87 | } |
| 44 | 88 | } |
| 89 | ||
| 90 | module.exports = { Garden }; | |
| 91 | } |