8kyu
Coding 3min : Jumping Dutch act
This is the simple version of Shortest Code series. If you need some challenges, please try the challenge version
Task:
Mr. despair wants to jump off Dutch act, So he came to the top of a building.
Scientific research shows that a man jumped from the top of the roof, when the floor more than 6, the person will often die in an instant; When the floor is less than or equal to 6, the person will not immediately die, he would scream. (without proof)
Input: floor, The height of the building (floor)
Output: a string, The voice of despair(When jumping Dutch act)
Example:
sc(2) should return "Aa~ Pa! Aa!"
It means:
Mr. despair jumped from the 2 floor, the voice is "Aa~"
He fell on the ground, the voice is "Pa!"
He did not die immediately, and the final voice was "Aa!"
sc(6) should return "Aa~ Aa~ Aa~ Aa~ Aa~ Pa! Aa!"
sc(7) should return "Aa~ Aa~ Aa~ Aa~ Aa~ Aa~ Pa!"
sc(10) should return "Aa~ Aa~ Aa~ Aa~ Aa~ Aa~ Aa~ Aa~ Aa~ Pa!"
if floor<=1, Mr. despair is safe, return ""
The final advice
Just play in this kata, Don't experiment in real life ;-)
function sc(floor){
let arr = [];
if( 1 < floor){
for(let i = 1 ; i <= floor ; i ++ ){
i !== floor ? arr.push("Aa~") : arr.push("Pa!");
}
if(floor <= 6){
arr.push("Aa!");
}
return arr.join(" ");
}else{
return "";
}
}
function sc(floor){
if(floor <= 1) return "";
return 'Aa~ '.repeat(floor-1) + 'Pa!' + (floor<=6 ? ' Aa!': '');
}
배열을 push()로 각각 넣는방법도 있지만, repeat()로 문자열을 합해주는 메서드를 사용하면
간단하게 문자열을 붙일 수 있다.