Finished
33// convenience to get you started writing code faster.
44//
55
6export const translate = () => {
7 throw new Error('Remove this line and implement the function');
6export const translate = (phrase) => {
7 // Edge Case: Empty input
8 if (!phrase) return '';
9
10 // Edge Case: Empty string input
11 if (phrase === '') return '';
12
13 const words = phrase.split(' ');
14 const vowels = ['a', 'e', 'i', 'o', 'u'];
15
16 const translateWord = (word) => {
17 // Edge Case: Empty word
18 if (!word) return '';
19
20 // Edge Case: Word with only punctuation
21 if (!/[a-zA-Z]/.test(word)) return word;
22
23 // Extract the actual word and any trailing punctuation
24 const match = word.match(/^([a-zA-Z]+)([^a-zA-Z]*)$/);
25 let cleanWord, punctuation;
26
27 // Edge Case: Word with no alphabetic characters
28 if (!match) {
29 return word; // Return as is if no match
30 }
31
32 cleanWord = match[1];
33 punctuation = match[2] || '';
34
35 const lowerWord = cleanWord.toLowerCase();
36
37 // Rule 1: If word begins with a vowel, or starts with "xr" or "yt"
38 // Edge Case: Words starting with "xr" or "yt"
39 if (vowels.includes(lowerWord[0]) || lowerWord.startsWith('xr') || lowerWord.startsWith('yt')) {
40 return cleanWord + 'ay' + punctuation;
41 }
42
43 // Rule 3: If word starts with consonants followed by "qu"
44 // Edge Case: Words with "qu" after consonants
45 let i = 0;
46 while (i < lowerWord.length && !vowels.includes(lowerWord[i])) {
47 if (i < lowerWord.length - 1 && lowerWord[i] === 'q' && lowerWord[i + 1] === 'u') {
48 // Move all consonants up to and including "qu" to the end
49 return cleanWord.substring(i + 2) + cleanWord.substring(0, i + 2) + 'ay' + punctuation;
50 }
51 i++;
52 }
53
54 // Rule 4: If word starts with consonants followed by "y"
55 // Edge Case: Words with "y" after consonants (but "y" not at the beginning)
56 i = 0;
57 while (i < lowerWord.length && !vowels.includes(lowerWord[i])) {
58 if (lowerWord[i] === 'y' && i > 0) {
59 // Move all consonants up to but not including "y" to the end
60 return cleanWord.substring(i) + cleanWord.substring(0, i) + 'ay' + punctuation;
61 }
62 i++;
63 }
64
65 // Rule 2: If word begins with one or more consonants
66 // Edge Case: Words starting with single or multiple consonants
67 i = 0;
68 while (i < lowerWord.length && !vowels.includes(lowerWord[i])) {
69 i++;
70 }
71
72 // Edge Case: All consonants (theoretically shouldn't happen with valid English words)
73 if (i === lowerWord.length) {
74 return cleanWord + 'ay' + punctuation;
75 }
76
77 return cleanWord.substring(i) + cleanWord.substring(0, i) + 'ay' + punctuation;
78 };
79
80 return words.map(translateWord).join(' ');
81
82 // Handled Edge Cases: Empty input, empty string, empty word, word with only punctuation,
83 // word with no alphabetic characters, words starting with "xr" or "yt",
84 // words with "qu" after consonants, words with "y" after consonants,
85 // words starting with single or multiple consonants, all consonants
886};
Test NameStatus
word beginning with a
Pass
word beginning with e
Pass
word beginning with i
Pass
word beginning with o
Pass
word beginning with u
Pass
word beginning with a vowel and followed by a qu
Pass
word beginning with p
Pass
word beginning with k
Pass
word beginning with x
Pass
word beginning with q without a following u
Pass
word beginning with consonant and vowel containing qu
Pass
word beginning with ch
Pass
word beginning with qu
Pass
word beginning with qu and a preceding consonant
Pass
word beginning with th
Pass
word beginning with thr
Pass
word beginning with sch
Pass
word beginning with yt
Pass
word beginning with xr
Pass
y is treated like a consonant at the beginning of a word
Pass
y is treated like a vowel at the end of a consonant cluster
Pass
y as second letter in two letter word
Pass
a whole phrase
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.