Created
August 24, 2022 18:09
-
-
Save thawkin3/478557e76ad0d75438a1d41afca8780f to your computer and use it in GitHub Desktop.
This file contains 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
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