본문 바로가기
Happly Coding/CodeWars

[CodeWars] 8kyu#7 - Short Long Short

by Hello Do. 2016. 5. 30.

[CodeWars] 8kyu#7 - Short Long Short


■ Description:


Given 2 strings, a and b, return a string of the form short+long+short, with the shorter string on the outside and the longer string on the inside. 

The strings will not be the same length, but they may be empty (length0).


solution("1", "22") // returns "1221"

solution("22", "1") // returns "1221"

solution("1", "22") // returns "1221"

solution("22", "1") // returns "1221"


■ Qustion:

1
2
3
function solution(a, b){
  // your code here
}
cs


■ My Solution:

1
2
3
4
5
6
7
function solution(a, b){
    var test = [a,b];
    var minValue = Math.min.apply(null, test);
    var maxValue = Math.max.apply(null, test);
    return minValue+''+maxValue+''+minValue;
} 
cs

정렬 관련 함수가 있을까? 검색해보니 Math에서 큰 수와 작은 수를 제공 해주는게 있어서 사용해 봄!!


■ Best Solution:

1
2
3
4
5
6
7
8
9
function solution(a, b) {
  return a.length < b.length ? a + b + a : b + a + b
}    
    
function solution(a, b){
    var short = a.length <= b.length ? a : b;
    var long = b.length >=a.length  ? b :a;
    return short  + long + short;
}
cs

삼항 연산자로 간단히 풀었네.. 으허허허허 당신은 천재!!