/
Pig-Latin.js
42 lines (32 loc) · 958 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
38
39
40
41
42
/*
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.
Input strings are guaranteed to be English words in all lowercase.
*/
function translatePigLatin(str) {
//Firs lets get that the first letter
var i,j,addedWord,vowelAtIndex;
var vowels = ["a","e","i", "o", "u"];
var letters = str.split("");
//lets check if is a consonant or not
for(j=0; j < letters.length; j++){
for(i = 0; i < vowels.length; i++){
if(vowels[i] == letters[j]){
if(vowelAtIndex === undefined){
vowelAtIndex = j;
break;
}
}
}
}
addedWord = str.slice(0, vowelAtIndex);
str = str.slice(vowelAtIndex, str.length);
if(vowelAtIndex === 0){
str = str + "way";
} else {
str = str + addedWord + "ay";
}
return str;
}
translatePigLatin("glove");