| 25 | 25 | C: 'clover', |
| 26 | 26 | }; |
| 27 | 27 | |
| 28 | | export class Garden { |
| 28 | class Garden { |
| 29 | 29 | constructor(diagram, students = DEFAULT_STUDENTS) { |
| 30 | | throw new Error('Remove this line and implement the function'); |
| 30 | // Edge Case: Empty diagram |
| 31 | if (!diagram) { |
| 32 | this.rows = ['', '']; |
| 33 | } else { |
| 34 | this.rows = diagram.split('\n'); |
| 35 | // Edge Case: Diagram with only one row |
| 36 | if (this.rows.length === 1) { |
| 37 | this.rows.push(''); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // Edge Case: Empty students array |
| 42 | if (!students || students.length === 0) { |
| 43 | this.students = [...DEFAULT_STUDENTS]; |
| 44 | } else { |
| 45 | this.students = [...students].sort(); |
| 46 | } |
| 31 | 47 | } |
| 32 | 48 | |
| 33 | 49 | plants(student) { |
| 34 | | throw new Error('Remove this line and implement the function'); |
| 50 | // Edge Case: Student not in the list |
| 51 | if (!this.students.includes(student)) { |
| 52 | return []; |
| 53 | } |
| 54 | |
| 55 | const studentIndex = this.students.indexOf(student); |
| 56 | const startPosition = studentIndex * 2; |
| 57 | |
| 58 | const plantCodes = []; |
| 59 | |
| 60 | // Get plants from first row |
| 61 | if (this.rows[0].length > startPosition) { |
| 62 | plantCodes.push(this.rows[0][startPosition]); |
| 63 | } |
| 64 | if (this.rows[0].length > startPosition + 1) { |
| 65 | plantCodes.push(this.rows[0][startPosition + 1]); |
| 66 | } |
| 67 | |
| 68 | // Get plants from second row |
| 69 | if (this.rows.length > 1 && this.rows[1].length > startPosition) { |
| 70 | plantCodes.push(this.rows[1][startPosition]); |
| 71 | } |
| 72 | if (this.rows.length > 1 && this.rows[1].length > startPosition + 1) { |
| 73 | plantCodes.push(this.rows[1][startPosition + 1]); |
| 74 | } |
| 75 | |
| 76 | // Edge Case: Invalid plant codes |
| 77 | return plantCodes.map(code => { |
| 78 | if (PLANT_CODES[code]) { |
| 79 | return PLANT_CODES[code]; |
| 80 | } else { |
| 81 | // Handle invalid codes by returning undefined or a default value |
| 82 | return 'unknown'; |
| 83 | } |
| 84 | }).filter(plant => plant !== 'unknown'); |
| 35 | 85 | } |
| 36 | 86 | } |
| 87 | |
| 88 | module.exports = { Garden }; |