단어 s의 가운데 글자를 반환하는 함수, solution을 만들어 보세요.
단어의 길이가 짝수라면 가운데 두글자를 반환하면 됩니다.
s는 길이가 1 이상, 100이하인 스트링입니다.
s | return |
---|---|
"abcde" | "c" |
"qwer" | "we" |
function solution(word) {
if (word.length === 1) return word;
const wordIndex = Math.floor(word.length / 2);
return word.length % 2 === 0
? word[wordIndex - 1] + word[wordIndex]
: word[wordIndex];
}