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