Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Get the Middle Character

/*
  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:

  getMiddle("test") should return "es"
  getMiddle("testing") should return "t"
  getMiddle("middle") should return "dd"
  getMiddle("A") should return "A"

  #Output
  The middle character(s) of the word represented as a string.
*/


const getMiddle = s => {
  const odd_even = s.length % 2
  const division = odd_even === 0 ? s.length / 2 : (s.length - 1) / 2
  return s.length === 1 ? s 
  	: odd_even === 0 ? s.substr(division - 1, 2) 
  	: s.substr(division, 1)
}

// With love @kouqhar
Source by www.codewars.com #
 
PREVIOUS NEXT
Tagged: #Get #Middle #Character
ADD COMMENT
Topic
Name
6+6 =