Dark Mode
Enable Console
getMiddle(string)
/*
Return the middle character in a string
If length is even, return middle character + next character
*/
getMiddle("codewars");
// "ew"
getMiddle("automobiles");
// "o"
Solution by - Need4Swede
function getMiddle(s){
// If even, return middle + next
if (s.length % 2 === 0){
return s = (s[s.length / 2 - 1]) + (s[s.length / 2]);
}
// Else return middle
return s[(s.length - 1) / 2]
}
Try it yourself - Codewars Source