Skip to content

Instantly share code, notes, and snippets.

@haase1020
Last active December 6, 2021 08:08
Show Gist options
  • Save haase1020/ba33cb7209134c754951335ccdd3db3b to your computer and use it in GitHub Desktop.
Save haase1020/ba33cb7209134c754951335ccdd3db3b to your computer and use it in GitHub Desktop.
clean house recursive optimized
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