Last active
June 15, 2018 17:57
-
-
Save eday69/8377b198d5a937b752700bff89c7942b to your computer and use it in GitHub Desktop.
freeCodeCamp Intermediate Algorithm Scripting: 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
// 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. | |
// Input strings are guaranteed to be English words in all lowercase. | |
// This is my solution: | |
function translatePigLatin(str) { | |
let letters=str.match(/^[^aeiou\s]*/); | |
if (letters != "") { | |
str=str.replace(/^[^aeiou]*/,""); | |
str=str.concat(letters,"ay"); | |
} | |
else { | |
str=str.concat("way"); | |
} | |
return str; | |
} | |
// But found this super cool solution (but is "y" a vowel?): | |
function translatePigLatin(str) { | |
return str | |
.replace(/^([aeiouy])(.*)/, '$1$2way') | |
.replace(/^([^aeiouy]+)(.*)/, '$2$1ay'); | |
} | |
translatePigLatin("consonant"); | |
translatePigLatin("cnsnnt"); | |
translatePigLatin("eight"); | |
translatePigLatin("algorithm"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment