Javascript String Get word middle character

Description

Javascript String Get word middle character

 P: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"

function getMiddle(s)
{
  const i = Math.floor(s.length / 2);
  let result = s[i];  
  if(s.length % 2 === 0 && i > 0) {
    result = s[i-1] + result;//from   w  w  w  . j  a  v  a  2s.com
  }
  return result;
}

const action = getMiddle("c");
console.log(action);



PreviousNext

Related