everycode icon indicating copy to clipboard operation
everycode copied to clipboard

2014年12月16日 D5

Open nunnly opened this issue 11 years ago • 3 comments

每个首字母位移到单词末位,然后在每个单词末尾添上ay PS:只能添加一行代码

function pigIt(str){
  //Code here
}
pigIt('Pig latin is cool'); //igPay atinlay siay oolcay

nunnly avatar Dec 16 '14 05:12 nunnly

啊,看错题目了,首字母移到末位,然后添加ay。 改正后:

function pigIt(str){
    return str.split(' ').map(function(v){return v.slice(1).split('').join('') + v[0] + 'py' }).join(' ');
}

// 测试用例
console.log(pigIt('Pig latin is cool')); //igPay atinlay siay oolcay

think2011 avatar Dec 16 '14 05:12 think2011

function pigIt(str) {
    return str.split(" ").map(function(v) {
        return v.trim().substr(1) + v.trim()[0] + "ay";
    }).join(" ");
}

console.log(pigIt('Pig latin is cool')); //igPay atinlay siay oolcay

XadillaX avatar Dec 16 '14 06:12 XadillaX

 function pigIt(str){
        return str.trim().replace(/\S{1,}/g,function(w){return [w.substr(1), w.substr(0,1),'ay'].join('')})
    }

singone avatar Dec 17 '14 07:12 singone