FCC Bonfire Series 123: Pig Latin

For this challenge, we are going to write a function that translates text to Pig Latin. Pig latin takes the first consonant -or consonant cluster- of a word and places it at the end of it, and then adds the ‘ay’ suffix. If the word starts with a bowel, we simply add ‘way’ at the end of it. Here’s a few examples:

translate('translation'); //-> 'anslationtray'
translate('google'); //-> 'ooglegay'
translate('orange'); //-> 'orangeway'

We need a way to differenciate between vowels and consonants, and that screams of regular expressions. We’ll use a regular expression that matches vowels and act as needed. Here’s what a first prototype would look like:

function translate(string) {
  string = string.split('');           // We turn the string into an array.
  var vowelMatch = /[aeiou]/;          // Vowel matching pattern.

  if (vowelMatch.test(string[0])) {    // If the first letter is a vowel,
    return string.join('') + 'way';    // return the original string with an
  }                                    // appended 'way'.

  // Move consonant cluster to end of the string and append 'ay'.

We now return every word starting with a vowel properly, but still need to move the initial consonant / consonant cluster to the end of the string when said string starts with a consonant.

We are going to create a while loop that runs until it finds a vowel. Every iteration should move the first letter -a consonant- to the end of the string:

function translate(string) {
  string = string.split('');
  var vowelMatch = /[aeiou]/;

  if (vowelMatch.test(string[0])) {
    return string.join('') + 'way';

  while (true) {
    if (!vowelMatch.test(string[0])) {
      string.push(string.splice(0, 1));
    } else {
  return string.join('') + 'ay';

And there we go! Ready for translation! Stay tuned, I’ll be back with a new exercise solution on monday!