본문 바로가기
알고리즘/Codewars

[Codewars] Are You Playing Banjo? (8 kyu) / JavaScript

by fluss 2022. 10. 17.

https://www.codewars.com/kata/53af2b8861023f1d88000832

 

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:

Create a function which answers the question "Are you playing banjo?".
If your name starts with the letter "R" or lower case "r", you are playing banjo!

The function takes a name as its only argument, and returns one of the following strings:

name + " plays banjo" 
name + " does not play banjo"

Names given are always valid strings.

 

설명:

"밴조를 연주하고 있나요?"라는 질문에 대답하는 함수를 만드세요.

만약 이름이 "R" 또는 소문자 "r"로 시작한다면 밴조를 연주하는 것입니다!

함수는 이름을 유일한 인자로 받고 다음 문자 열중 하나를 반환합니다:

 

name + " plays banjo" 
name + " does not play banjo"

 

이름은 언제나 유효한 문자열로 주어집니다.

 

풀이

function areYouPlayingBanjo(name) {
  if(name[0].match(/[Rr]/)) return name + " plays banjo"
  return name + " does not play banjo";
}

match를 이용해 문자열 name의 첫번째 글자가 "R"이나 "r"인지 확인하고 그에 따른 결과 값을 반환했다.

 

참고

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/match

 

String.prototype.match() - JavaScript | MDN

match() 메서드는 문자열이 정규식과 매치되는 부분을 검색합니다.

developer.mozilla.org

 

댓글