Last active
October 28, 2022 21:43
-
-
Save 0D1NTR33/e65a5b18e7ce5a448dfe6ab80cdb4304 to your computer and use it in GitHub Desktop.
Get the Middle Character (JavaScript)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Awesome | |
function getMiddle(s) | |
{ | |
return s.substr(Math.ceil(s.length / 2 - 1), s.length % 2 === 0 ? 2 : 1); | |
} | |
// Usual | |
function GetMiddle(s) | |
{ | |
var middle = Math.floor(s.length/2); | |
if (s.length % 2 == 0) | |
return s[middle-1] + s[middle]; | |
else | |
return s[middle]; | |
} | |
/* | |
Kata.getMiddle("test") should return "es" | |
Kata.getMiddle("testing") should return "t" | |
Kata.getMiddle("middle") should return "dd" | |
Kata.getMiddle("A") should return "A" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the explications :)