-
Notifications
You must be signed in to change notification settings - Fork 335
Expand file tree
/
Copy path06 Bornfire Pig Latin.js
More file actions
44 lines (33 loc) · 1.1 KB
/
06 Bornfire Pig Latin.js
File metadata and controls
44 lines (33 loc) · 1.1 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
/* Bonfire: Pig Latin
Difficulty: 2/5
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.
Remember to use RSAP if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
Array.indexOf()
Array.push()
Array.join()
String.substr()
String.split()
Code by Rafael Rodriguez
rafase282@gmail.com
http://www.freecodecamp.com/rafase282
*/
function translate(str) {
// Create variables to be used
var pigLatin = '';
var regex = /[aeiou]/gi;
// Check if the first character is a vowel
if (str[0].match(regex)) {
pigLatin = str + 'way';
} else {
// Find how many consonants before the first vowel.
var vowelIndice = str.indexOf(str.match(regex)[0]);
// Take the string from the first vowel to the last char
// then add the consonants that were previously omitted and add the ending.
pigLatin = str.substr(vowelIndice) + str.substr(0, vowelIndice) + 'ay';
}
return pigLatin;
}