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

[Codewars] Mumbling (7 kyu) / JavaScript

by fluss 2022. 10. 28.

https://www.codewars.com/kata/5667e8f4e3f572a8f2000039

 

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:

This time no story, no theory. The examples below show you how to write function accum:

 

Examples:

accum("abcd") -> "A-Bb-Ccc-Dddd"
accum("RqaEzty") -> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
accum("cwAt") -> "C-Ww-Aaa-Tttt"

The parameter of accum is a string which includes only letters from a..z and A..Z.

 

설명:

이번에는 이야기도 이론도 없습니다. 아래의 예시는 함수 accum을 작성하는 법을 알려드립니다:

 

예시:

accum("abcd") -> "A-Bb-Ccc-Dddd"
accum("RqaEzty") -> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
accum("cwAt") -> "C-Ww-Aaa-Tttt"

accum의 매개변수는 a에서 z 그리고 A에서 Z까지의 문자만을 포함하는 문자열입니다.

 

풀이

반복문을 이용해서 문자열을 반복시켜주었고 문자열의 첫 번째 위치를 대문자로 바꿔주었다.

 

코드

function accum(s) {
  s = s.toLowerCase().split('');
  for(let i = 0; i < s.length; i++){
    s[i] = s[i].repeat(i + 1);
    s[i] = s[i].charAt(0).toUpperCase() + s[i].slice(1);
  }
  return s.join('-');
}

댓글