Skip to content

Commit 06e5319

Browse files
committedDec 8, 2017
found a recursive reverse string
1 parent 3b54c57 commit 06e5319

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed
 

‎src/challenges.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,22 @@ const cacheFunction = cb => {
3636

3737
/* ======================== Recursion Practice ============================ */
3838
const reverseStr = str => {
39-
return str.split('').reverse().join('');
39+
var first = str[0];
40+
var last = str[str.length -1];
41+
var str1 = reverseStr(str.substring(1, str.length - 1));
42+
return last + str1 + first;
4043
};
44+
// based on https://stackoverflow.com/questions/4859208/recursive-string-reversal-function-in-javascript
4145

4246
const checkMatchingLeaves = obj => {
4347
// return true if every property on `obj` is the same
4448
// otherwise return false
4549
};
4650

4751
const flatten = elements => {
48-
each()
52+
each(elements, element => { // call each function to iterate over the array
53+
elements = flatten([].concat([], ...elements)); // flatten by breaking out nested arrays as we recursively call flatten function
54+
});
4955
};
5056

5157
module.exports = {

0 commit comments

Comments
 (0)
Please sign in to comment.