Description:
Move the first letter of each word to the end of it, then add 'ay' to the end of the word.
console.log(pigIt('Pig latin is cool')); // igPay atinlay siay oolcay
- my answer:使用正则
function pigIt(str){
return str.replace(/(\w)(\w*)(\s|$)/g, "$2$1ay$3")
}
function pigIt(str){
return str.replace(/\b(\w)(\w+)\b/ig,"$2$1ay");
}
- other answer:
function pigIt(str){
return str.split(' ').map(function(el){
return el.slice(1) + el.slice(0,1) + 'ay';
}).join(' ');
}