Last active
July 6, 2021 16:50
-
-
Save danny-andrews/2ac9bceb762f197b0c827f2a4324c03c to your computer and use it in GitHub Desktop.
Revisions
-
danny-andrews revised this gist
Jul 6, 2021 . 1 changed file with 10 additions and 38 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -7,42 +7,14 @@ const reverseIterative = (arr) => { return result; }; const reverseRec = (arr) => arr.length === 0 ? [] : arr.slice(-1).concat(reverseRec(arr.slice(0, -1))); // reverseRec([1, 2, 3]); // [3] + reverseRec([1, 2]) // [3] + ([2] + reverseRec([1])) // [3] + ([2] + ([1] + reverseRec([]))) // [3] + ([2] + ([1] + [])) // [3] + ([2] + [1]) // [3] + [2, 1] // [3, 2, 1] -
danny-andrews revised this gist
Jul 6, 2021 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -39,3 +39,10 @@ const input = [1, 2, 3, undefined, 4, 5]; methods.forEach((method) => { console.log(method(input)); }); // reverseSlice([1, 2, 3]); // [3] + reverseSlice([1, 2]) // [3] + [2] + reverseSlice([1]) // [3] + [2] + [1] + reverseSlice([]) // [3] + [2] + [1] + [] // [3, 2, 1] -
danny-andrews created this gist
Jul 6, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ const reverseIterative = (arr) => { const result = []; for (let i = arr.length - 1; i >= 0; i--) { result.push(arr[i]); } return result; }; const reverseMutating = (arr) => { if (arr.length === 0) return []; const last = arr.pop(); return [last].concat(reverseMutating(arr)); }; const reverseSlice = (arr) => arr.length === 0 ? [] : arr.slice(-1).concat(reverseSlice(arr.slice(0, -1))); const reverseES6 = ([head, ...tail]) => { if (!head) return []; return [...reverseES6(tail), head]; }; const reverseTurboMode = ([head, ...tail]) => !head ? [] : [...reverseTurboMode(tail), head]; const methods = [ reverseIterative, reverseSlice, reverseES6, reverseTurboMode, reverseMutating, ]; const input = [1, 2, 3, undefined, 4, 5]; methods.forEach((method) => { console.log(method(input)); });