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

Commit 0251f0f

Browse files
mfuellbierKent C. Dodds
authored andcommitted
feat(revstring): Add revstring function (#99)
Closes #98
1 parent 5305731 commit 0251f0f

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import revstring from './revstring'
12
import initArray from './init-array'
23
import reduce from './reduce-to-tally'
34
import flatten from './flatten'
@@ -34,6 +35,7 @@ import round from './round'
3435
import checkPalindrome from './checkPalindrome'
3536

3637
export {
38+
revstring,
3739
initArray,
3840
reduce,
3941
flatten,

src/revstring.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default revstring
2+
3+
/**
4+
* Original Source: https://stackoverflow.com/a/35211814
5+
*
6+
* This method will revers the order of a the chars in a string.
7+
*
8+
* @param {String} str - string whose order to be reversed
9+
* @returns {String} - string with reversed order
10+
*/
11+
12+
function revstring(str) {
13+
let newString = ''
14+
for (let i = str.length; i >= 0; i--) {
15+
newString += str.charAt(i)
16+
}
17+
return newString
18+
}

test/revstring.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import test from 'ava'
2+
import {revstring} from '../src'
3+
4+
test('string with only letters', t => {
5+
const str = "abcdefG"
6+
const expected = "Gfedcba"
7+
const result = revstring(str)
8+
t.is(expected, result)
9+
})
10+
11+
test('string with letters and special characters', t => {
12+
const str = 'abc!"§'
13+
const expected = '§"!cba'
14+
const result = revstring(str)
15+
t.is(expected, result)
16+
})

0 commit comments

Comments
 (0)