-
Notifications
You must be signed in to change notification settings - Fork 0
/
recrusion.js
35 lines (32 loc) · 855 Bytes
/
recrusion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const someKey = (box) => box.some(
item => item === 'key'
? true // base case
: item.length && someKey(item) // base then recrusive case
)
// const someKey = (box) => {
// for (const item of box) {
// if (item === 'key') return true
// if (item.length) return someKey(item)
// return false
// }
// }
// const someKey = (pile) => {
// let isKeyFound = false
//
// // untill key is found or boxes are exosted
// while (!isKeyFound && pile.length) {
// const box = pile.shift() // tackle a first item
//
// if (box.length) { // if box isn't empty
// for (let item of box) {
// if (item === 'key') {
// isKeyFound = true
// }
// pile.push(item) // put to the pile
// }
// }
// }
//
// return isKeyFound
// }
console.log(someKey([[[], [[]], [], 'key' ]]))