-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPigLatin.js
More file actions
60 lines (33 loc) · 1.28 KB
/
PigLatin.js
File metadata and controls
60 lines (33 loc) · 1.28 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function translatePigLatin(str) {
var stringToTranslate = '';
stringToTranslate = str;
var wordArray = stringToTranslate.split(' ');
wordArray = wordArray.map(translateWord);
str = wordArray.join(' ');
return str;
}
console.log(translatePigLatin("consonants are awesome to have"));
function startsWithAVowel(word) { return /[aeiouyAEIOUY]/.test(word.charAt(0)); }
function translateWord (word) {
if (startsWithAVowel(word)) {
word += 'way';
return word;
} else {
// locate first consonate group, cut it, move it to end
for (var i = 0; i < word.length; i++) {
if (startsWithAVowel(word.charAt(i))) {
var newWord = word.substr(i);
var consonantClusterToMove = word.substr(0, i);
newWord = newWord + consonantClusterToMove + 'ay';
return newWord;
}
}
}
}
// 1) take sentence, divide into word array
// 2) take off period from last word if there is one
// 3) loop through array
// a) test for starting with vowel, if so modigy accordingly
// b) test for starting with consonant, if so modify accordingly
// 4) assemble array of words to a string
// 5) add period if there was one to begin with