Created
January 22, 2019 18:16
-
-
Save anuraagdjain/380f79b18a17e8bb6ea9f021c056eff8 to your computer and use it in GitHub Desktop.
convert strings into "Pig latin"
This file contains hidden or 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
const vowelRegex = new RegExp('^[aeiou]', 'g'); | |
function pigLatin(str){ | |
if (vowelRegex.test(str)) { | |
return `${str}way`; | |
} else { | |
const first = str.slice(0, 1); | |
const rem = str.slice(1, str.length); | |
return `${rem}${first}ay`; | |
} | |
} | |
const testInputs = ['paragraphs', 'california', 'algorithm']; | |
testInputs.forEach(function (ip) { | |
console.log(pigLatin(ip)); | |
}); | |
// output | |
// aragraphspay | |
// aliforniacay | |
// algorithmway |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment