Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Get the Middle Character

//codewars:Get the Middle Character
function getMiddle(str)
{
   let length = str.length
   if(length <=0 || !str)return ""
   if(length%2==0){

       var result = str.charAt(Math.floor(length/2)-1)+str.charAt(Math.floor(length/2))
   }else  var result = str.charAt(Math.floor(length/2))
        
   return result

}
Comment

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
Comment

PREVIOUS NEXT
Code Example
Javascript :: socket io add timeout 
Javascript :: of() angular 
Javascript :: javascript submit form VUE 
Javascript :: test one function in react class 
Javascript :: sequelize documentation 
Javascript :: next js generate pdf complete 
Javascript :: how to make a field not required with joi 
Javascript :: update karma jasmine to specific version 
Javascript :: node js arabic number to english number 
Javascript :: good way to check object properties in js 
Javascript :: multiple image upload react 
Javascript :: redirect using expressjs 
Javascript :: material ui navbar 
Javascript :: js date format 
Javascript :: jse api 
Javascript :: usestate hook with callback 
Javascript :: js pop matched value in array 
Javascript :: interactive svg javascript 
Javascript :: Detect Pangram 
Javascript :: js event handlers 
Javascript :: iterate over array of object javascript and access the properties 
Javascript :: make shorter if statements with objects 
Javascript :: canvas setup 
Javascript :: openstreetmap api example javascript 
Javascript :: react onchange url 
Javascript :: click binding angular 8 
Javascript :: angular indexeddb 
Javascript :: js export options 
Javascript :: axios get array of urls 
Javascript :: upload bloob javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =