Pig Latin - JavaScript Solution & Walkthrough
(06/21) Learn how to solve coding challenges using FreeCodeCamp's curriculum.
06/21 Pig Latin
Pig Latin is a way of altering English Words. The rules are as follows:
If a word begins with a consonant, take the first consonant or consonant cluster, move it to the end of the word, and add ay to it.
If a word begins with a vowel, just add way at the end.
Translate the provided string to Pig Latin. Input strings are guaranteed to be English words in all lowercase.
function translatePigLatin(str) {
return str;
}
translatePigLatin("consonant");
Credit: FreeCodeCamp.org
Understanding the Challenge
At first glance, this challenge might seem a bit tricky. So let's try to break it down and see it what it requires.
The translatePigLatin()
function will be passed a string. The challenge here is to complete the function such that it alters the given string using the Pig Latin rules.
Before we proceed, we need to understand the rules of Pig Latin. Fortunately, the challenge explains it for us.
First, if a word begins with a vowel, we just add "way" to the end. The following letters are the vowels of the English alphabets "a", "e", "i", "o", "u". For example, the word eight
begins with "e" which is a vowel
Therefore if the word eight
is passed as an argument to our function, it should return eight
+ way
which eightway
.
However, if the word begins with a consonant, which any of the other letters apart from the six vowels. Then, we take the consonants cluster. The consonant cluster will be all the letters that comes before the first vowel
in that string. For example, the word glove
has a consonant cluster of gl
. These two g
and l
are the letters that comes before the first vowel which in this case is o
. So we take these conosonants cluster, send it
to the end of the word and add "ay" to it. Let's take a look at an example to make it clear.
Given the word schwartz
, our function should return artzschway
.
Here's why. The first letter is s
which is a consonant. Thus we take the consonant cluster. In this schw
. These are all the consonants that comes before the first vowel which a
.
Then we'll artz
+ schw
+ ay
which gives us our final answer of artzschway
.
I encourage you to read through the explanation again if its still not clear. Once you're ready, let's move on.
Pseudocode
Given a string,
Check whether the first letter is a vowel or consonant
if it is a vowel
add "ay" to the end
Return the result
if it is a consonant
remove the consonant cluster before the first vowel of the word
add the consonant cluster to the end of the word
add "ay" to it
Return the result
Solving the Challenge
First, we create an array constaining all the vowels.
Next, we'll create a variable called pigLatin
with an initial value of an empty string.
We'll also create another variable cluster
with an initial value of an empty string. This will hold the consonant cluster if the word begins with a consonant.
const vowels = ["a", "e", "i", "o", "u"];
let pigLatin = "";
let consonants = "";
Next, we'll check if the string starts with a vowel. We can do so by using .includes()
to check if the vowels
array includes or contain the first letter of the string which str[0].
If it does, then we just adds the "way"
to the given string.
if (vowels.includes(str[0])) {
pigLatin = str + "way"
}
However, if it does not, then we use the else
statement.
Inside the else
statement, we will use a for loop
to loop through the individual letters of the string. For each letter, we want to find out whether or not it is included in the vowels
array.
If it is a consonant we just concatenate it to the cluster
variable.
if (vowels.includes(str[0])) {
pigLatin = str + "way";
} else {
for(let i = 0; i < str.length; i++) {
if (!vowels.includes(str[i])) {
cluster += str[i];
}
Once we meet the first vowel in the string, we will have our consonant cluster ready. We use the .substring()
method to get the remaining letters. And then we add our consonant cluster
plus "ay"
. See the code below.
if (vowels.includes(str[0])) {
pigLatin = str + "way";
} else {
for(let i = 0; i < str.length; i++) {
if (!vowels.includes(str[i])) {
cluster += str[i];
} else {
pigLatin = str.substring(i) + cluster + "ay";
break;
}
Note that we break out of the loop once we get to the first vowel in the string.
Finally after we need to cater for a situation where the given string will be made of consonants only. In that case, we only add "ay"
to the given str
if (vowels.includes(str[0])) { pigLatin = str + "way"; } else { for(let i = 0; i < str.length; i++) { if (!vowels.includes(str[i])) { cluster += str[i]; } else { pigLatin = str.substring(i) + cluster + "ay"; break; } pigLatin = str + "ay"; } }
We can now return pigLatin
and our function is complete!
return pigLatin;
Final Solution
function translatePigLatin(str) {
const vowels = ["a", "e", "i", "o", "u"];
let pigLatin = "";
let cluster = "";
if (vowels.includes(str[0])) {
pigLatin = str + "way";
} else {
for(let i = 0; i < str.length; i++) {
if (!vowels.includes(str[i])) {
cluster += str[i];
} else {
pigLatin = str.substring(i) + cluster + "ay";
break;
}
pigLatin = str + "ay";
}
}
return pigLatin;
}
console.log(translatePigLatin("rhythm"));
Congratulations!
You just cracked the Pig Latin challenge. Cheers and happy coding!