Skip to content

Instantly share code, notes, and snippets.

@haase1020
Last active December 6, 2021 08:06
Show Gist options
  • Save haase1020/af6d0f7f3a93a9a11a017ab3519f8999 to your computer and use it in GitHub Desktop.
Save haase1020/af6d0f7f3a93a9a11a017ab3519f8999 to your computer and use it in GitHub Desktop.
clean house recursive function
const todoList = [
"pick up the floor",
"clear the table",
"put away dishes",
"sweep",
"clean the bathrooms",
"dust",
"mop",
];
function cleanHouse(list) {
console.log(list); // notice the array changes with each recursive call
if (list.length === 0) {
console.log("Great job! House cleaning is done ✨");
return; // you need this return to stop running the function!
}
cleanHouse(list.slice(1));
}
console.log(cleanHouse(todoList));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment