| 8 | 8 | * @return {string} |
| 9 | 9 | */ |
| 10 | 10 | export const translate = (text) => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 11 | // Edge Case: Empty string input |
| 12 | if (!text || text.length === 0) { |
| 13 | return ''; |
| 14 | } |
| 15 | |
| 16 | // Split text into words |
| 17 | const words = text.split(' '); |
| 18 | |
| 19 | // Process each word |
| 20 | const translatedWords = words.map(word => { |
| 21 | // Edge Case: Empty word (multiple spaces) |
| 22 | if (word.length === 0) { |
| 23 | return word; |
| 24 | } |
| 25 | |
| 26 | // Edge Case: Word starts with vowel or special cases "xr" or "yt" |
| 27 | if (/^[aeiou]|^xr|^yt/.test(word)) { |
| 28 | return word + 'ay'; |
| 29 | } |
| 30 | |
| 31 | // Edge Case: Word starts with consonants followed by "qu" |
| 32 | const quMatch = word.match(/^([^aeiou]*qu)(.*)/); |
| 33 | if (quMatch) { |
| 34 | return quMatch[2] + quMatch[1] + 'ay'; |
| 35 | } |
| 36 | |
| 37 | // Edge Case: Word starts with consonants followed by "y" |
| 38 | const yMatch = word.match(/^([^aeiou]+)y(.*)/); |
| 39 | if (yMatch) { |
| 40 | return 'y' + yMatch[2] + yMatch[1] + 'ay'; |
| 41 | } |
| 42 | |
| 43 | // Edge Case: Word starts with consonants (general case) |
| 44 | const consonantMatch = word.match(/^([^aeiou]+)(.*)/); |
| 45 | if (consonantMatch) { |
| 46 | return consonantMatch[2] + consonantMatch[1] + 'ay'; |
| 47 | } |
| 48 | |
| 49 | // Edge Case: Fallback (should not happen with valid input) |
| 50 | return word + 'ay'; |
| 51 | }); |
| 52 | |
| 53 | return translatedWords.join(' '); |
| 12 | 54 | }; |
| 55 | |
| 56 | // Handled Edge Cases: Empty string input, Empty word (multiple spaces), Word starts with vowel or special cases "xr" or "yt", Word starts with consonants followed by "qu", Word starts with consonants followed by "y", Word starts with consonants (general case), Fallback case |