Last active
December 6, 2021 08:08
-
-
Save haase1020/ba33cb7209134c754951335ccdd3db3b to your computer and use it in GitHub Desktop.
clean house recursive optimized
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 characters
const todoList = [ | |
"pick up the floor", | |
"clear the table", | |
"put away dishes", | |
"sweep", | |
"clean the bathrooms", | |
"dust", | |
"mop", | |
]; | |
//optimized cleanHouse: no copies are made! | |
function cleanHouseOptimized(list) { | |
return _cleanHouse(list, 0); | |
} | |
function _cleanHouse(array, index) { | |
console.log(array); // notice here the array is unchanged | |
if (index === array.length) { | |
console.log("Great job! House cleaning is done."); | |
return; // you need this return to stop running the function! | |
} | |
return _cleanHouse(array, index + 1); | |
} | |
console.log(cleanHouseOptimized(todoList)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment