Skip to content

Instantly share code, notes, and snippets.

@thawkin3
Created August 24, 2022 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thawkin3/478557e76ad0d75438a1d41afca8780f to your computer and use it in GitHub Desktop.
Save thawkin3/478557e76ad0d75438a1d41afca8780f to your computer and use it in GitHub Desktop.
export const findKeyInBox = (box) => {
console.log(`Looking in Box ${box.id}`);
for (let i = 0; i < box.contents.length; i++) {
if (box.contents[i].type === 'key') {
console.log(`Found the key in Box ${box.id}!`);
return box.id;
}
if (box.contents[i].type === 'box') {
console.log(`Found Box ${box.contents[i].id} in Box ${box.id}`);
const boxContainingKey = findKeyInBox(box.contents[i]);
if (boxContainingKey) {
return boxContainingKey;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment