-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06-pig-latin.js
More file actions
42 lines (34 loc) · 894 Bytes
/
06-pig-latin.js
File metadata and controls
42 lines (34 loc) · 894 Bytes
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
function translatePigLatin(str) {
var vowels = [];
vowels.push(str.indexOf("a"));
vowels.push(str.indexOf("e"));
vowels.push(str.indexOf("i"));
vowels.push(str.indexOf("o"));
vowels.push(str.indexOf("u"));
var lowIndex = 0;
var compare = str.length + 1;
vowels.forEach(function(val, i){
if (val < compare && val > -1) {
lowIndex = i;
compare = val;
}
});
var spliceEnd = null;
if (lowIndex === 0) {
spliceEnd = str.indexOf("a");
} else if (lowIndex === 1) {
spliceEnd = str.indexOf("e");
} else if (lowIndex === 2) {
spliceEnd = str.indexOf("i");
} else if (lowIndex === 3) {
spliceEnd = str.indexOf("o");
} else if (lowIndex === 4) {
spliceEnd = str.indexOf("u");
}
if (spliceEnd === 0) {
str = str + "way";
} else {
str = str.substr(spliceEnd) + str.slice(0, spliceEnd) + "ay";
}
return str;
}