-
-
Save nirajkrz/3eedfd52c3c6017063ef to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Pig Latin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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