← Back to challenges

Return the Middle Character(s) of a String

JavaScriptHardstringsformattingmathregex

Instructions

Create a function that takes a string and returns the middle character(s). If the word's length is odd, return the middle character. If the word's length is even, return the middle two characters.

Examples

getMiddle("test") ➞ "es"

getMiddle("testing") ➞ "t"

getMiddle("middle") ➞ "dd"

getMiddle("A") ➞ "A"

Notes

All test cases contain a single word (as a string).

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Adding Parity Bits