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

[Codewars] If you can't sleep, just count sheep!! (8 kyu) / JavaScript

by fluss 2022. 11. 4.

https://www.codewars.com/kata/5b077ebdaf15be5c7f000077

 

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:

If you can't sleep, just count sheep!!

 

Task:

Given a non-negative integer, 3 for example, return a string with a murmur: "1 sheep...2 sheep...3 sheep...". Input will always be valid, i.e. no negative integers.

 

설명:

잠들 수 없다면 양을 세세요!!

 

작업:

음수가 아닌 정수를 받아, 예를 들어 3이면 중얼거리는 소리의 문자열을 반환하세요: "1 sheep... 2 sheep... 3 sheep...". 입력은 항상 유효합니다. 즉 음수인 정수는 없습니다

 

풀이

 반복문으로 숫자를 하나씩 늘려가면서 숫자 뒤에 sheep...을 붙인 문자열을 배열에 담은 다음 출력한다.

 

코드

var countSheep = function (num){
  let result = []
  for(let i = 1; i <= num; i++){
    result.push(i + ' sheep...');
  }
  return result.join('');
}

댓글