-
Notifications
You must be signed in to change notification settings - Fork 0
/
pig_latin
33 lines (29 loc) · 863 Bytes
/
pig_latin
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
function translate(string){
return string
.toLowerCase()
.split(' ')
.map(translateWord)
.map(addAY)
.join(',');
}
function translateWord(words){
var vowels = { a:'a', e:'e', i:'i', o:'o', u:'u'};
for (var k in vowels){
if(checkPhoneme(vowels[k],words)){ //phoneme 'qu'
return words.slice(words.indexOf(vowels[k])+1).concat(words.slice(0,words.indexOf(vowels[k]+1)));
}else if(checkVowel(vowels[k],words)){ //vowel
return words;
}else{ //all other consonants
return words.slice(words.indexOf(vowels[k])).concat(words.slice(0,words.indexOf(vowels[k])));
}
}
}
function checkVowel(vowel,words){
return words.slice(0,1) === vowel;
}
function checkPhoneme(vowels,words){
return words.indexOf(vowels.u) === (words.indexOf('q') + 1);
}
function addAY(word){
return word += 'ay';
}