This repository was archived by the owner on Feb 20, 2019. It is now read-only.
File tree 4 files changed +30
-1
lines changed
4 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 101
101
" kentcdodds/ava"
102
102
]
103
103
}
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'
68
68
import fibonacciSum from './fibonacciSum'
69
69
import lcm from './lcm'
70
70
import occurrences from './occurrences'
71
+ import getMiddle from './getMiddle'
71
72
72
73
export {
73
74
reverseArrayInPlace ,
@@ -140,4 +141,5 @@ export {
140
141
fibonacciSum ,
141
142
lcm ,
142
143
occurrences ,
144
+ getMiddle ,
143
145
}
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