Write a program that prints the middle letter of a word (string) in lowercase letters when it is entered. However, if the length of a word is even, the middle two characters are printed.
A string is entered on the first line. The length of the string does not exceed 100.
study
good
Prints the middle character on the first line.
u
oo
slice()
function solution(s) {
let answer;
if (s.length % 2 !== 0) {
answer = s.slice(Math.floor(s.length / 2), Math.floor(s.length / 2) + 1);
} else {
answer = s.slice(s.length / 2 - 1, s.length / 2 + 1);
}
return answer;
}
substring()
function solution(s) {
let answer;
let mid = Math.floor(s.length / 2);
if (s.length % 2 === 1) answer = s.substring(mid, mid + 1);
else answer = s.substring(mid - 1, mid + 1);
return answer;
}
substr()
(c.f. splice()
for Array)function solution(s) {
let answer;
let mid = Math.floor(s.length / 2);
if (s.length % 2 === 1) answer = s.substr(mid, 1);
else answer = s.substr(mid - 1, 2);
return answer;
}