Skip to content

Instantly share code, notes, and snippets.

@anuraagdjain
Created January 22, 2019 18:16
Show Gist options
  • Save anuraagdjain/380f79b18a17e8bb6ea9f021c056eff8 to your computer and use it in GitHub Desktop.
Save anuraagdjain/380f79b18a17e8bb6ea9f021c056eff8 to your computer and use it in GitHub Desktop.
convert strings into "Pig latin"
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