Pig Latin javascript

Pig Latin Challenge in JavaScript

Introduction

Pig Latin is a coding challenge in which we must add rules to words based on their characteristics. We will have a function with one argument str that holds the input word. Our first task is to verify whether the given word starts with a consonant or a vowel.

In the alphabet from A to Z, all letters are known as consonants except for the vowels a, e, i, o, and u.

Vowels: A, E, I, O, U

Consonants: All other letters (B, C, D, F, G, H, J, K, L, M, N, P, Q, R, S, T, V, W, X, Y, Z)

If the word starts with a consonant, then we need to truncate all the initial letters until a vowel letter appears. Attach all initial letters that were truncated to the end of the word, and finally add ay. And if the word start with any vowel letter, then we just need to add way at the end of that given word. Don’t get confused, I will make it easy for you in the upcoming steps.

For example, If the word start with a consonant, let's take the word glove. First, we remove all the consonant letters until we reach a vowel letter. After removing the consonant letters, we have the output ove. Next, we add all the removed consonant letters from the initial phase to the end of the word. So, our word becomes ovegl since the removed letters were gl. Finally, we add ay at the end of the word, resulting in the correct output oveglay.

And if the word start with a vowel, for instance, we have the word eight, then we just need to add way at the end of this word, and our effective output will be eightway.

Hopefully, you understood the whole mystery behind this challenge if there is any confusion you still have it will be cleared after solving this challenge.

Theoretically solve:

Step 1: We need to determine whether the word starts with a consonant or a vowel.

Step 2: We need to remove all the initial letters until a vowel letter appears If the word start with a consonant, add all the letters that were removed in our previous phase at the end of the word, and finally add ay.

Step 3: We need to just add way if the word starts with a vowel letter.

Practically Solve:

JavaScript

copy

 function translatePigLatin(str) {
   let result = '';
   let consonant = '';
   let remaining = '';
   let vowels = 'aeiou';


   for (let i = 0; i < str.length; i++) {
     if (vowels.includes(str[i])) {
       break
     }


     consonant += str[i];
   }


   remaining += str.slice(consonant.length);


   if (vowels.includes(str.split('').shift())) {
     result += str + "way";
   } else {
     result += remaining + consonant + "ay";
   }


   return result;
 }


 console.log(translatePigLatin("glove"));

Let’s break down this solution for better understanding.

Don’t get confused, I will explain every line of code that is written above.

So we have a function named translatePigLatin with one parameter str (as mentioned earlier, where our word will be stored). This function is straightforward, so no deep dive is needed.

JavaScript

copy

 let result = '';
 let consonant = '';
 let remaining = '';
 let vowels = 'aeiou';

Firstly, we declared a few variables and left all of them empty for now, except the vowels variable.

The variable result is for the final output.

The variable consonant is used to save the initial letters in case the word starts form a consonant. Since we will use these initial letters at the end of the word in our next step.

The variable remaining is used for getting the rest of the word’s letters after removing the initial letters by using consonant.

In our variable Vowels, we assigned all the vowel letters. This variable is used in the condition for verifying whether the given word’s first letter matches all of them (the variable vowels). If it is matched, then we will use our logic accordingly.

JavaScript

copy

for(let i  = 0; i < str.length; i++) {
  if(vowels.includes(str[i])) {
    break
  }
   
  consonant += str[i];
 }

After that, in our second step, we started a for loop for accessing all the letters of the given word. Our purpose of this loop is just to get the initial consonant letters that we will truncate later if the word start with a consonant. For this reason, we used an if else condition. Remember this step is only for getting the initial consonants, our word won’t be truncated yet, we will do it in our next step.

JavaScript

copy

if(vowels.includes(str[i])) {
  break
}

It will ensure that if the word starts with a consonant, it will continue collecting every letter that is truncated until a vowel letter appears. Since the loop will start from the first letter and will go step by step into the next letter as the vowel letter appears, it will break the loop because of the break keyword.

JavaScript

copy

consonant += str[i];

In JavaScript, += is used for updating the value of the variable.

Inside the loop, you can see the line of code shown above. It will assign all the initial truncated consonants into the consonant variable, which will be useful for us while attaching it at the end of the word later.

JavaScript

copy

remaining += str.slice(consonant.length);

We have all the initial consonant letters in our consonant variable, but our word is still the same because we did our previous step just to get the consonants. Now we will truncate the word by using the consonant variable.

We will use a JavaScript built-in method called slice(), which is used for truncating the strings It starts truncating the string from the first letter by accepting a number as an argument (so if our word is hello, and we use 'hello'.slice(2) it will return llo by truncating the initial 2 letters as we asked it to do.

So we used str.slice(consonant.length), which will truncate the first initial letters since we used our consonant.length as an argument, which means that as much as our consonant variable length will be, it will truncate only those words.

For instance, we have the word glove so in our first step we collected all the initial consonant letters by using a for loop. We assigned them to the variable consonant, then our word will be the same glove but now our consonant variable has this value gl, in our next step we will truncate this string by using slice() method we will add 'glove'.slice(consonant.length) then it will return ove since our consonant variable is this gl which means the length is 2.

JavaScript

copy

if(vowels.includes(str.split('').shift())) {
  result += str+"way";
} else {
  result += remaining + consonant +"ay";
}

Now, We need to use the if-else condition to return our final value accordingly, within our if else condition we used our vowels variable that will confirm whether the first letter of the given word is a vowel or not if the first letter is matched any of the letters of vowels variable then it's mean it is a vowel.

For achieving this, we used the includes() method to verify that all the letters of the vowels variable match with the first letter of the given word.

Inside includes() method we used our word (str) since we only need to compare the first letter of the word (str), so we need to convert this word(str) into an array to get the first letter because we have a built-in method shift() (shift() is a JavaScript Array method used for getting first element from the array),

Finally, within our if-else statement, we assigned our updated value within the result variable, which means if the word (str) first letter is a vowel, then it should return the same word (str) with way at the end.

In our else condition (that will be only true if the first condition isn’t true), we return the result += remaining + consonant +"ay"; result variable by updating the values within it.

We store the truncated word in the variable remaining, the initial consonants in consonant, and then form the final word by adding remaining + consonant + 'ay', as required by the challenge.

All Coding Challenges