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('');
}
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Sum of Digits / Digital Root (6 kyu) / JavaScript (0) | 2022.11.07 |
---|---|
[Codewars] Stop gninnipS My sdroW! (6 kyu) / JavaScript (0) | 2022.11.05 |
[Codewars] Calculate average (8 kyu) / JavaScript (0) | 2022.11.03 |
[Codewars] The Supermarket Queue (6 kyu) / JavaScript (0) | 2022.11.02 |
[Codewars] Count of positives / sum of negatives (8 kyu) / JavaScript (0) | 2022.11.01 |
댓글