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

[Codewars] Abbreviate a Two Word Name (8 kyu) / JavaScript

by fluss 2022. 12. 27.

https://www.codewars.com/kata/57eadb7ecd143f4c9c0000a3

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website 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:

Write a function to convert a name into initials. This kata strictly takes two words with one space in between them.

 

The output should be two capital letters with a dot separating them.

It should look like this:

Sam Harris => S.H

patrick feeney => P.F

 

설명:

이름을 이니셜로 바꾸는 함수를 작성하세요. 이 카타는 엄격하게 사이에 공백이 하나 들어간 두 단어만을 받습니다.

 

출력은 점으로 구분되는 두 개의 대문자여야 합니다.

다음과 같아야 합니다:

Sam Harris => S.H

patrick feeney => P.F

 

풀이

입력받은 name을 모두 대문자로 바꾸고 공백을 기준으로 잘랐다. 그리고 자른 단어의 앞글자를 '.'을 기준으로 합쳐주었다.

 

코드

function abbrevName(name){
  name = name.toUpperCase().split(' ');
  return name[0][0] +"." + name[1][0];
}

댓글