Skip to content

Instantly share code, notes, and snippets.

@duggiemitchell
Created January 18, 2016 17:47
Show Gist options
  • Select an option

  • Save duggiemitchell/afe9047021080037f8ba to your computer and use it in GitHub Desktop.

Select an option

Save duggiemitchell/afe9047021080037f8ba to your computer and use it in GitHub Desktop.
Pig Latin
# Pig Latin
Translate the provided string to pig latin.
Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an "ay".
If a word begins with a vowel you just add "way" to the end.
function translate(str) {
var RegEx = /[aeiouAEIOU]/;
var latinTranslation ="";
var vowelIndex ="";
if (str[0].match(RegEx)) {
latinTranslation = str + "way";
} else {
// Find how many consonants before the first vowel.
vowelIndex = str.indexOf(str.match(RegEx)[0]);
console.log(vowelIndex); // vowel is at index[1]
// Take the string from the first vowel to the last char
// then add the consonants that were previously omitted and add the ending.
latinTranslation = str.substr(vowelIndex) + str.substr(0, vowelIndex) + 'ay';
}
return latinTranslation;
}
translate("consonant");
translate("algorithm");

Pig Latin

Translate the provided string to pig latin. Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an "ay". If a word begins with a vowel you just add "way" to the end.

function translate(str) {
  var RegEx = /[aeiouAEIOU]/;
  var latinTranslation ="";
  var vowelIndex ="";
  if (str[0].match(RegEx)) {
      latinTranslation = str + "way";
  }  else  {
       // Find how many consonants before the first vowel.
    vowelIndex = str.indexOf(str.match(RegEx)[0]);
    console.log(vowelIndex); // vowel is at index[1]
    // Take the string from the first vowel to the last char
    // then add the consonants that were previously omitted and add the ending.
    latinTranslation = str.substr(vowelIndex) + str.substr(0, vowelIndex) + 'ay';
  }
  return latinTranslation;
}
translate("consonant");
translate("algorithm");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment