https://www.codewars.com/kata/520b9d2ad5c005041100000f
Codewars - Achieve mastery through coding practice and developer mentorship
Coding practice for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
DESCRIPTION:
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 !
설명:
각 단어의 첫 글자를 그 단어의 끝으로 옮기고 끝에 "ay"를 더해줍니다. 구두점은 그대로 놔두세요.
예시
pigIt('Pig latin is cool'); // igPay atinlay siay oolcay
pigIt('Hello world !'); // elloHay orldway !
풀이
function pigIt(str){
let reg = /[.,\/#?!$%\^&\*;:{}=\-_`~()]/;
str = str.split(' ').map(el => el.match(reg) === null ? el.slice(1) + el.slice(0, 1) + "ay" : el);
return str.join(' ');
}
입력된 문자열을 공백으로 나눠서 구두점이 아닐 경우 첫 글자를 뺀 단어와 첫 글자를 합쳐주고 "ay"를 붙인다.
다른 사람의 좋았던 풀이
function pigIt(str){
return str.replace(/(\w)(\w*)(\s|$)/g, "\$2\$1ay\$3")
}
참고
https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Regular_Expressions
정규 표현식 - JavaScript | MDN
정규 표현식, 또는 정규식은 문자열에서 특정 문자 조합을 찾기 위한 패턴입니다. JavaScript에서는 정규 표현식도 객체로서, RegExp의 exec()와 test() 메서드를 사용할 수 있습니다. String의 match(), matchA
developer.mozilla.org
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/replace
String.prototype.replace() - JavaScript | MDN
replace() 메서드는 어떤 패턴에 일치하는 일부 또는 모든 부분이 교체된 새로운 문자열을 반환합니다. 그 패턴은 문자열이나 정규식(RegExp)이 될 수 있으며, 교체 문자열은 문자열이나 모든 매치에
developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes
Character classes - JavaScript | MDN
Character classes distinguish kinds of characters such as, for example, distinguishing between letters and digits.
developer.mozilla.org
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Even or Odd (8 kyu) / JavaScript (0) | 2022.10.02 |
---|---|
[Codewars] Convert boolean values to strings 'Yes' or 'No'. (8 kyu) / JavaScript (0) | 2022.10.01 |
[Codewars] Beginner Series #3 Sum of Numbers (7 kyu) / JavaScript (0) | 2022.09.30 |
[Codewars] Binary Addition (7 kyu) / JavaScript (0) | 2022.09.29 |
[Codewars] Descending Order (7 kyu) / JavaScript (0) | 2022.09.28 |
댓글