The Middle Letter

Problem

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.

input

A string is entered on the first line. The length of the string does not exceed 100.

study
good

output

Prints the middle character on the first line.

u
oo

Solution

using 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;
}

using 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;
}

using 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;
}