Created
March 28, 2020 22:30
-
-
Save sillyfellow/8eb7d7e19172adb725b5f60d3a8154ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function flatten(arr) { | |
if (arr.length == 0) { | |
return []; | |
} | |
const first = arr[0]; | |
const flatRest = flatten(arr.slice(1)); | |
if (!Array.isArray(first)) { | |
return [first].concat(flatRest); | |
} | |
return flatten(first).concat(flatRest); | |
} | |
const tests = [ | |
[], | |
[1], | |
[1, 2], | |
[1, 2, [3]], | |
[1, 2, [3], [4, [5, [6]]]], | |
[[[1], 2, 3], 4, 5, [[[6, 7], 8]]], | |
]; | |
tests.forEach(arr => { | |
console.log(arr, " --> ", flatten(arr)); | |
}); | |
function sReverse(s) { | |
if (s.length < 2) { | |
return s; | |
} | |
return sReverse(s.substring(1)) + s[0]; | |
} | |
["", "1", "12", "123"].forEach(s => { | |
console.log(s, " --> ", sReverse(s)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
flatten a list (recursively)
reverse a string (recursively)
in javascript