Created
August 24, 2022 18:09
-
-
Save thawkin3/d9b650b119e85626c839e64f5de5aab9 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) => { | |
const pile = []; | |
pile.push(box); | |
while (pile.length > 0) { | |
const currentBox = pile.pop(); | |
console.log(`Looking in Box ${currentBox.id}`); | |
for (let i = 0; i < currentBox.contents.length; i++) { | |
if (currentBox.contents[i].type === 'key') { | |
console.log(`Found the key in Box ${currentBox.id}!`); | |
return currentBox.id; | |
} | |
if (currentBox.contents[i].type === 'box') { | |
console.log( | |
`Found Box ${currentBox.contents[i].id} in Box ${currentBox.id}` | |
); | |
pile.push(currentBox.contents[i]); | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment