Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 7808a04

Browse files
d-ivashchukKent C. Dodds
authored and
Kent C. Dodds
committed
feat(getMiddle): add getMiddle function (#189)
My first pull request! Had to use --no-verify because some error didn't let me commit the changes though. Thanks again for the opportunity!
1 parent f58b735 commit 7808a04

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,4 @@
101101
"kentcdodds/ava"
102102
]
103103
}
104-
}
104+
}

Diff for: src/getMiddle.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

Diff for: src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import makeObjectIterable from './makeObjectIterable'
6868
import fibonacciSum from './fibonacciSum'
6969
import lcm from './lcm'
7070
import occurrences from './occurrences'
71+
import getMiddle from './getMiddle'
7172

7273
export {
7374
reverseArrayInPlace,
@@ -140,4 +141,5 @@ export {
140141
fibonacciSum,
141142
lcm,
142143
occurrences,
144+
getMiddle,
143145
}

Diff for: test/getMiddle.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
})

0 commit comments

Comments
 (0)