-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpig-latin.js
37 lines (28 loc) · 896 Bytes
/
pig-latin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
Translate the provided string to pig latin.
Pig Latin takes the first consonant (or consonant cluster) of an English word,
moves it to the end of the word and suffixes an "ay".
If a word begins with a vowel you just add "way" to the end.
If a word does not contain a vowel, just add "ay" to the end.
Input strings are guaranteed to be English words in all lowercase.
*/
function translatePigLatin(str) {
let startVowel = /^[aeiou]/i;
let globalVowels = /[aeiou]/gi;
if (startVowel.test(str)) {
str = str.concat("way");
} else if (globalVowels.test(str)) {
for (let i = 0; i < str.length - 1; i++) {
if (!startVowel.test(str)) {
str = str.slice(1, str.length).concat(str.charAt(0));
} else {
break;
}
}
str = str.concat("ay");
} else {
str = str.concat("ay");
}
return str;
}
console.log(translatePigLatin("glove"));