
소문자로 된 단어(문자열)가 입력되면 그 단어의 가운데 문자를 출력하는 프로그램을 작성하세요. 단 단어의 길이가 짝수일 경우 가운데 2개의 문자를 출력합니다.
- 첫 줄에 문자열이 입력된다. 문자열의 길이는 100을 넘지 않습니다.
- 첫 줄에 가운데 문자를 출력합니다.
- 입력 예) study
good - 출력 예) u
oo
풀이
const solution = (str) => {
let result
const isOdd = str.length%2===0
if(!isOdd){
result = str.split("")[Math.floor(str.length/2)]
}else{
result = str.split("")[Math.floor(str.length/2)]+str.split("")[Math.floor(str.length/2+1)]
}
return result
}
solution("study")'Algorithm' 카테고리의 다른 글
| [algorithm] 중복 단어 제거 (0) | 2022.11.20 |
|---|---|
| [algorithm] 중복 문자 제거 (0) | 2022.11.20 |
| [algorithm] 가장 긴 문자열 (0) | 2022.11.20 |
| 대소문자 변환 (0) | 2022.11.20 |
| [algorithm] 대문자로 통일 (0) | 2022.11.20 |