본문 바로가기
코테연습

31.Replace With Alphabet Position

by hxunz 2022. 4. 7.

Description:

Welcome.

In this kata you are required to, given a string, replace every letter with its position in the alphabet.

If anything in the text isn't a letter, ignore it and don't return it.

"a" = 1, "b" = 2, etc.

Example

alphabetPosition("The sunset sets at twelve o' clock.")

Should return "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11" ( as a string )

 


 

function alphabetPosition(text) {
  const words = text.toLowerCase().replace(/[^a-z]/g, '');
  const alphabet = [...words].map(w=> w.charCodeAt(0) - 97+1);
  return alphabet.join(' ');
}

먼저, 주어진 텍스트를 전부 소문자로 변경하고 a부터 z까지 일치하는 알파벳이 있으면 대체하였다.

 

그리고 알파벳을 숫자로 나타내는 방법은 구글링을 해서 찾아보았는데 charCodeAt()을 이용하였다. 

 

그런 다음에 words를 배열로 만든 다음에 map을 사용해서 알파벳과 일치하는 숫자를 넣어주었다. 

 

그 다음에는 숫자를 공백기준으로 스트링으로 만들었다. 

'코테연습' 카테고리의 다른 글

33.Tribonacci Sequence  (0) 2022.04.07
32.Your order, please  (0) 2022.04.07
30.Persistent Bugger.  (0) 2022.04.04
29.Take a Ten Minutes Walk  (0) 2022.04.04
28.Duplicate Encoder  (0) 2022.04.04

댓글