Skip to content

Instantly share code, notes, and snippets.

@nirajkrz
Forked from anonymous/bonfire-pig-latin.js
Created December 3, 2015 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nirajkrz/3eedfd52c3c6017063ef to your computer and use it in GitHub Desktop.
Save nirajkrz/3eedfd52c3c6017063ef to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Pig Latin
// Bonfire: Pig Latin
// Author: @nirajkrz
// Challenge: http://www.freecodecamp.com/challenges/bonfire-pig-latin
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function translate(str) {
// Create variables to be used
var pigLatin = '';
var regex = /[aeiou]/gi;
// Check if the first character is a vowel
if (str[0].match(regex)) {
pigLatin = str + 'way';
} else {
// Find how many consonants before the firs vowel.
var vowelIndice = str.indexOf(str.match(regex)[0]);
// Take the string from the first vowel to the last char
// then add the consonants that were previously omitted and add the ending.
pigLatin = str.substr(vowelIndice) + str.substr(0, vowelIndice) + 'ay';
}
return pigLatin;
}
translate("consonant");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment