| 8 | 8 | * @returns {string[]} |
| 9 | 9 | */ |
| 10 | 10 | export const rows = (input) => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 11 | // Edge Case: empty input or null input |
| 12 | if (!input) { |
| 13 | return []; |
| 14 | } |
| 15 | |
| 16 | // Edge Case: invalid input (non-alphabetic characters) |
| 17 | if (!/^[A-Za-z]$/.test(input)) { |
| 18 | return []; |
| 19 | } |
| 20 | |
| 21 | // Edge Case: single letter input ('A') |
| 22 | const letter = input.toUpperCase(); |
| 23 | if (letter === 'A') { |
| 24 | return ['A']; |
| 25 | } |
| 26 | |
| 27 | const startCharCode = 'A'.charCodeAt(0); |
| 28 | const targetCharCode = letter.charCodeAt(0); |
| 29 | const n = targetCharCode - startCharCode; |
| 30 | |
| 31 | // Edge Case: maximum letter input (beyond 'Z') |
| 32 | if (n < 0 || n > 25) { |
| 33 | return []; |
| 34 | } |
| 35 | |
| 36 | const diamond = []; |
| 37 | const width = 2 * n + 1; |
| 38 | |
| 39 | // Generate top half including middle row |
| 40 | for (let i = 0; i <= n; i++) { |
| 41 | const currentChar = String.fromCharCode(startCharCode + i); |
| 42 | const outerSpaces = n - i; |
| 43 | |
| 44 | if (i === 0) { |
| 45 | // First row: only one 'A' |
| 46 | const row = ' '.repeat(outerSpaces) + 'A' + ' '.repeat(outerSpaces); |
| 47 | diamond.push(row); |
| 48 | } else { |
| 49 | // Other rows: two identical letters with inner spaces |
| 50 | const innerSpaces = 2 * i - 1; |
| 51 | const row = ' '.repeat(outerSpaces) + currentChar + ' '.repeat(innerSpaces) + currentChar + ' '.repeat(outerSpaces); |
| 52 | diamond.push(row); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Generate bottom half (mirror of top half, excluding middle row) |
| 57 | for (let i = n - 1; i >= 0; i--) { |
| 58 | diamond.push(diamond[i]); |
| 59 | } |
| 60 | |
| 61 | return diamond; |
| 12 | 62 | }; |
| 63 | |
| 64 | // Handled Edge Cases: empty input or null input, invalid input (non-alphabetic characters), single letter input ('A'), maximum letter input (beyond 'Z') |