Free Code Camp Pig Latin Algorithm Solution

jackLyons Free Code Camp Tags:
0

In this post I will describe the method I used to solve the Free Code Camp Pig Latin Algorithm challenge. This was quite a challenging problem for me as it involved quite a few moving parts. However, it was really worth the pain because it taught me a lot about string manipulation, which is quite a common task in day-to-day web development work. 

To solve this challenge I used the following helpful hints (note, none of these were part of the FCC hints):

How to solve the challenge:

Step 1: Set your vowels

In order to speak Pig Latin you need to know where each vowel exists within a word. When a word starts with a vowel, then all you need to do is add way to the end of your word. For example: Apple => Appleway, Eight => Eightway. Simple enough to understand for now.

Of course, things do get more complex, but for now we can start by finding the vowels in a string. Basically, all you need to do is loop through the given string and search for a vowel. This can be achieved with the help of the charAt() method.


function translatePigLatin(str) {
  /* Set Vowels */
  let i = 0;
  const vowels = ["a", "e", "i", "o", "u"];
  /* Loop through the string */
  for (i; i < str.length; i++) {
	let vowel = str.charAt(i);
	if (vowel === "a" || vowel === "e" || vowel === "i" || vowel === "o" || vowel === "u") {
		break; // break the loop if a vowel is found, then continue to be manipulated
	}
  }
}

Step 2: Manipulate the string

Now comes the fun part. You can now try to slice up the string and add “way” to the end. That’s 50% of the battle accomplished. The other 50% is figuring out what to do if the given word begins with a consonant. In this case, you need to scan the string from the beginning until you find a vowel. When you do, the idea here is to slice the letters up to that vowel, and add them to the back of the string. You then add “ay” to the end of the sentence.

Let’s look at the two cases side by side:

Case 1: Vowel (apple => appleway, egg => eggway)

Case 2: Consonant (coffee => offeecay, pizza => izzapay)

In code, I wrote a simple if statement to check between the two cases.

/* Slice string after vowel */
  let strAfterVowel = str.slice(i);
  const consonantString = strAfterVowel + str.slice(0, i) + "ay";
  /* Speak Pig Latin */
  if (vowels.includes(str[0])) {
   // if vowel at beginning
    str = str + "way";
  } else {
   // if consonant at beginning
    str = consonantString;
  } 
  return str; 

Breaking down the logic

The variable strAfterVowel is a number of the exact position where the first vowel existed. The logic behind this is that when a vowel is found in the initial for loop above, the loop breaks. The variable is set at the index of the first vowel.

Therefore, the string after the first vowel begins at index i. The slice method will cut everything after index i which will be the rest of the string. The variable consonantString is the entire string including the string after the vowel plus the string before  the vowel. 

Now a simple if statement and we are done. Check if the string has a vowel with the help of the includes() method. Here’s what the final code will look like:

function translatePigLatin(str) {
	/* Set Vowels */
	let i = 0;
	const vowels = ["a", "e", "i", "o", "u"];
	/* Loop through the string */
	for (i; i < str.length; i++) {
		let vowel = str.charAt(i);
		if (vowel === "a" || vowel === "e" || vowel === "i" || vowel === "o" || vowel === "u") {
			break;
		}
	}
	/* Slice string after vowel */
	let strAfterVowel = str.slice(i);
	const consonantString = strAfterVowel + str.slice(0, i) + "ay";
	/* Speak Pig Latin */
	if (vowels.includes(str[0])) {
		// if vowel at beginning
		str = str + "way";
	} else {
		// if consonant at beginning
		str = consonantString;
	}
	return str;
}
translatePigLatin("pizza");

The Web Developers Bootcamp.

A n00b friendly guide to becoming a pro!

If you’re feeling a bit overwhelmed and a little lost with all these crazy hard Free Code Camp exercises then this course is for you!

While I am oh so grateful for the opportunity to learn through Free Code Camp, I still find that learning to code alone and without proper guidance extremely exhausting. Which is why I’ve started to substitute my education with an incredible online resource called the Web Developers Bootcamp courtesy of Udemy.

This course has helped me brush off the cobwebs, unlearn my self-taught bad habits and gain a solid understanding of the true fundamentals of web development. It has helped me see why it is important to understand these JavaScript problems and how they apply to the real world.

This bootcamp will teach you how built beautiful sites with Bootstrap, add in functionality with JavaScript and write back end code with Node.js all in a series of short, sharp, no BS video modules. I seriously could not recommend it any higher. Check it out and let me know what you think 🙂

The Web Developers Bootcamp
The Web Developer Bootcamp.
The only course you need to learn web development – HTML, CSS, JS, Node, and More!

Sign up to receive up-to-date web tips, tricks and code hacks!