단어 s의 가운데 글자를 반환하는 함수, solution 만들기.
단어의 길이가 짝수라면 가운데 두글자를 반환.
function solution(s) {
const ctrIdx = Math.floor(s.length / 2)
// 단어길이 홀수
if(s.length % 2 === 1) {
return s[ctrIdx]
}
// 단어길이 짝수
else {
return s[ctrIdx-1] + s[ctrIdx]
}
// ... 삼항연산자를 이용해서 하면
const ctrIdx = Math.floor(s.length / 2)
return s.length % 2 === 1 ? s[ctrIdx] : s[ctrIdx-1] + s[ctrIdx]
}
function solution(s) {
return s.substring(Math.ceil(s.length / 2) - 1, Math.floor(s.length / 2) + 1);
}
특정 위치 문자 찾기 2가지 방법 (charAt 함수, 대괄호)
https://hianna.tistory.com/378
MDN - String.prototype.substring()
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/substring
MDN - String.prototype.substr()
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/substr