Be Brav3

FCC: Pig Latin (JavaScript Intermediate Algorithm)

September 20, 2016

In Free Code Camp’s Pig Latin Challenge we need to convert English dictionary words to pig latin using the following rules:

  • Move the first set of consonants up to the first vowel to the end of the word Then add “ay” as the suffix If the first letter of the word is a vowel, then just add “way” as the suffix tot he word Here’s how I completed this challenge:

First create a variable to store the vowels as a string. Then create two variables to store whether a match to a vowel has been found and the string index of that vowel.

[js]

var vowel = “aeiou”;

var match = -1;

var index = -1;

[/js]

Both match and index are initialized as -1. The -1 for match is an error code returned by the function we’ll be using indicating a match is not found and it is also used in our loop condition for the loop to continue. Since index stores the location of the first vowel match a string index of -1 naturally can not happen in an actual string. Initializing it as -1 is therefore in hopes of catching any error in the code. It’s purely optional.

The next section finds a matching vowel in the string and stores the index of that vowel.

[js]

for (var x = 0; x < str.length && match < 0; x++) {

match = vowel.indexOf(str.charAt(x));

if (match >= 0) {

index = x;

}

}

[/js]

In the above loop, the loop condition makes sure we’re within the length of the string and that we haven’t found a match yet (match < 0). Then we take each character beginning with index 0 from the string and try to match it in the vowel string using the indexOf function and stores that index in match. We actually don’t care what the index is since this is getting the index position in vowel variable. But the usefulness of this statement is if a match is not found, the value will be a -1. This is a perfect way to check whether we should continue the loop.

Then we compare whether match has a none -1 value. If so then we found our vowel and stores the index of this vowel in x.

The next piece of code uses existing JavaScript functions to rearrange our string using the index we found earlier.

[js]

if (index > 0) {

str = str.slice(index) + str.substring(0, index) + “ay”;

} else {

str = str.slice(index) + str.substring(0, index) + “way”;

}

[/js]

First we have to differentiate when to tack on the “ay” or “way” suffix to the end of the pig latin. When the index equals zero, that means the first letter of the word is a vowel. So our if-else condition use > 0 to tack on the “ay” suffix. Otherwise it tacks on the “way” suffix.

Within the if-else condition we use JavaScript’s slice() and substring() functions to help us rearrange the letters. Slice creates a new string starting with an index. So we use the index of the vowel to start creating a new string. We don’t enter an end value so the entire string from the index to the end will be used to create a new string. Then substring function is used to create a string from the beginning consonant up to the first vowel. Finally we tack on the suffix and store all three strings in the str string variable.

The complete code is below:

[js]

function translatePigLatin(str) {

var vowel = “aeiou”;

var match = -1;

var index = -1;

// find index of first vowel

for (var x = 0; x < str.length && match < 0; x++) {

match = vowel.indexOf(str.charAt(x));

if (match >= 0) {

index = x;

}

}

if (index > 0) {

str = str.slice(index) + str.substring(0, index) + “ay”;

} else {

str = str.slice(index) + str.substring(0, index) + “way”;

}

return str;

}

console.log(translatePigLatin(“consonant”));

console.log(translatePigLatin(“california”));

console.log(translatePigLatin(“paragraphs”));

console.log(translatePigLatin(“glove”));

console.log(translatePigLatin(“algorithm”));

console.log(translatePigLatin(“eight”));

[/js]