This repository was archived by the owner on Feb 20, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +30
-1
lines changed Expand file tree Collapse file tree 4 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 101101 " kentcdodds/ava"
102102 ]
103103 }
104- }
104+ }
Original file line number Diff line number Diff line change 1+ export default getMiddle
2+
3+ /**
4+ * This method will return the character that stays in the middle of the string.
5+ *
6+ * @param {String } string - string to get the middle character from
7+ * @return {String } - middle character of the string
8+ **/
9+
10+ function getMiddle ( string ) {
11+ return string . substr ( Math . ceil ( string . length / 2 - 1 ) , string . length % 2 === 0 ? 2 : 1 )
12+ }
Original file line number Diff line number Diff line change @@ -68,6 +68,7 @@ import makeObjectIterable from './makeObjectIterable'
6868import fibonacciSum from './fibonacciSum'
6969import lcm from './lcm'
7070import occurrences from './occurrences'
71+ import getMiddle from './getMiddle'
7172
7273export {
7374 reverseArrayInPlace ,
@@ -140,4 +141,5 @@ export {
140141 fibonacciSum ,
141142 lcm ,
142143 occurrences ,
144+ getMiddle ,
143145}
Original file line number Diff line number Diff line change 1+ import test from 'ava'
2+ import { getMiddle } from '../src'
3+
4+ test ( 'Gets middle character of given string with a length of uneven number of characters ' , t => {
5+ const string = 'rumpelstiltskin'
6+ const expected = 't'
7+ const actual = getMiddle ( string )
8+ t . deepEqual ( actual , expected )
9+ } )
10+ test ( 'Gets two middle characters of given string with a length of even number of characters ' , t => {
11+ const string = 'pull'
12+ const expected = 'ul'
13+ const actual = getMiddle ( string )
14+ t . deepEqual ( actual , expected )
15+ } )
You can’t perform that action at this time.
0 commit comments