You are going to be given a word. Your job is to return the middle character of the word. If the word's length is odd, return the middle character. If the word's length is even, return the middle 2 characters.
My code:
function getMiddle(s) {
const length = s.length;
if (length % 2 === 0) {
return s[(length / 2) - 1] + s[length / 2];
} else {
return s[Math.floor(length / 2)];
}
}
function getMiddle(s)
{
return s.substr(Math.ceil(s.length / 2 - 1), s.length % 2 === 0 ? 2 : 1);
}
New things I learned today: