본문 바로가기
Algorithm

[JS Algorithm] 가운데 문자 출력

by 쾌횽 2023. 3. 22.
반응형

소문자로 된 단어(문자열)가 입력되면 그 단어의 가운데 문자를 출력하는 프로그램을 작성하세요.
단 단어의 길이가 짝수일 경우 가운데 2개의 문자를 출력합니다.

▣ 입력설명
첫 줄에 문자열이 입력된다.
문자열의 길이는 100을 넘지 않습니다.
▣ 출력설명
첫 줄에 가운데 문자를 출력합니다.
▣ 입력예제 1
study
▣ 출력예제 1
u
▣ 입력예제 2
good
▣ 출력예제 2
oo

function solution(s){  
  let answer = ""
  if(s.length % 2 === 0){
    answer += s[s.length / 2-1]
    answer += s[s.length / 2]
  }else {
    answer += s[Math.ceil(s.length / 2)-1]
  }
  return answer;
}
console.log(solution("study")); // u
반응형

'Algorithm' 카테고리의 다른 글

[JS Algorithm] 중복 단어 제거  (0) 2023.03.23
[JS Algorithm] 중복 문자 제거  (0) 2023.03.23
[JS Algorithm] 가장 긴 문자열  (0) 2023.03.22
[JS Algorithm] 대소문자 변환  (0) 2023.03.16
[JS Algorithm] 대문자로 통일  (0) 2023.03.16

댓글