Skip to content

Commit 7707918

Browse files
author
Luciano Medeiros Marcelino
committed
Cover case of an empty string in get-string-permutations
When adding a test case for an empty string, the code was looping endlessly.
1 parent 5345bd7 commit 7707918

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/_Problems_/get-string-permutations/get-string-permutations.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ describe('Get permutations of a string', () => {
2222
expect(getPermutations(shortString)).toEqual(expectedPermutations);
2323
});
2424

25+
it('returns an empty array for an empty string', () => {
26+
const shortString = '';
27+
const expectedPermutations = [];
28+
29+
expect(getPermutations(shortString)).toEqual(expectedPermutations);
30+
});
31+
2532
it('is case sensitive', () => {
2633
const shortString = 'aB';
2734

src/_Problems_/get-string-permutations/index.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
// GET PERMUTATION OF A GIVEN STRING
22

33
const getPermutations = (str) => {
4+
let result = [];
5+
6+
if (str.length == 0) {
7+
return result;
8+
}
9+
410
if (str.length == 1) {
5-
let array = [];
6-
array.push(str);
7-
return array;
11+
result.push(str);
12+
return result;
813
}
914

1015
let currentCharacter = str.charAt(0);
1116
let restOfString = str.substring(1);
12-
let result = [];
1317
let returnResult = getPermutations(restOfString);
1418

1519
for (j = 0; j < returnResult.length; j++) {

0 commit comments

Comments
 (0)