< 문제 >
풀이
- 주어진 문자열의 갯수가 짝수일때, 홀수일때 분기처리후 홀수일경우에는 2를 나눴을때 소수점이 나오기때문에 Math.floor로 소수점을 버리고 index를가지고 가운데있는 글자를 가져오는 풀이
function solution(s) {
let answer = '';
const floor = calculate => Math.floor(calculate);
if (s.length % 2 === 0) {
answer = s[floor(s.length / 2 - 1)] + s[floor(s.length / 2)];
} else {
answer = s[floor(s.length / 2)];
}
return answer;
}
console.log(solution('abcde'));
console.log(solution('qwer'));