알고리즘 88 - Simple Pig Latin

tamagoyakii·2022년 8월 10일
0

알고리즘

목록 보기
88/89

Q.

Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.

Examples
pigIt('Pig latin is cool'); // igPay atinlay siay oolcay
pigIt('Hello world !'); // elloHay orldway !

A)

function pigIt(str){
  return str
    .split(" ")
    .map(el => el.match(/^[.,:!?]/) ? el : (el + el[0] + "ay").slice(1))
    .join(" ")
}

0개의 댓글