Skip to content

Latest commit

 

History

History
43 lines (28 loc) · 1.25 KB

get-the-middle-character.md

File metadata and controls

43 lines (28 loc) · 1.25 KB

Get the Middle Character 7 Kyu

LINK TO THE KATA - FUNDAMENTALS STRINGS

Description

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.

Examples:

Kata.getMiddle("test") should return "es"

Kata.getMiddle("testing") should return "t"

Kata.getMiddle("middle") should return "dd"

Kata.getMiddle("A") should return "A"

Input

A word (string) of length 0 < str < 1000 (In javascript you may get slightly more than 1000 in some test cases due to an error in the test cases). You do not need to test for this. This is only here to tell you that you do not need to worry about your solution timing out.

Output

The middle character(s) of the word represented as a string.

Solution

const getMiddle = string => {
  const stringLength = string.length

  return stringLength % 2 !== 0
    ? string.substring(stringLength / 2, stringLength / 2 + 1)
    : string.substring(stringLength / 2 - 1, stringLength / 2 + 1)
}