Skip to content

Commit eeb227f

Browse files
committed
adding middle character
1 parent baf367b commit eeb227f

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

docs/middle-character.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# middle-character
2+
3+
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.
4+
5+
### Examples:
6+
7+
```js
8+
Kata.getMiddle("test")
9+
// => return "es"
10+
11+
Kata.getMiddle("testing")
12+
// => return "t"
13+
14+
Kata.getMiddle("middle")
15+
// => return "dd"
16+
17+
Kata.getMiddle("A")
18+
// => return "A"
19+
```
20+
21+
22+
## Install
23+
24+
```
25+
$ npm install @abranhe/codewars
26+
```
27+
28+
## Usage
29+
30+
```js
31+
const codewars = require('@abranhe/codewars');
32+
33+
codewars.getMiddle('test');
34+
//=> 'es'
35+
```
36+
37+
## API
38+
39+
### codewars.getMiddle(input)
40+
41+
#### input
42+
43+
Type: `string`

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ codewars.maskify('454233223424324');
2929
- [codewars.spinWords()](docs/spin-words.md)
3030
- [codewars.squareDigit()](docs/square-every-digit.md)
3131
- [codewars.complementaryDNA()](docs/complementary-dna.md)
32+
- [codewars.getMiddle()](docs/middle-character.md)
3233

3334
## License
3435

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const maskify = require('./credit-card-mask');
44
const spinWords = require('./spin-words');
55
const squareDigits = require('./square-every-digit');
66
const complementaryDNA = require('./complementary-dna');
7+
const getMiddle = require('./middle-character');
78

89
module.exports = {
910
maskify,
1011
spinWords,
1112
squareDigits,
12-
complementaryDNA
13+
complementaryDNA,
14+
getMiddle
1315
};

src/middle-character.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = s => {
2+
return s.slice((s.length - 1) / 2, (s.length / 2) + 1);
3+
};

tests/middle-character.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import test from 'ava';
2+
import {getMiddle} from '../src';
3+
4+
test('Check middle-character.js', t => {
5+
t.is(getMiddle('test'), 'es');
6+
t.is(getMiddle('testing'), 't');
7+
t.is(getMiddle('middle'), 'dd');
8+
t.is(getMiddle('A'), 'A');
9+
});

0 commit comments

Comments
 (0)