How to jump out of recursion?

has a tree data structure; suppose there are five branches; now I need to find a node; the node is on the third layer of the first branch; how can the subsequent trees not be traversed after finding this node? That is, jump out of the whole recursion? I wrote a simple test and found that I couldn"t jump out of recursion

.
var arr = [[1,2],[3,4]]
const deep = (arr) => {
    for (let i = 0; i < arr.length; iPP) {
        if (Array.isArray(arr[i])) {
            deep(arr[i])
        } else {
            console.log(arr[i])
            if (arr[i] === 2) return 
        }
    }
}
deep(arr)

or is recursion itself uninterruptible?

Feb.23,2022

add a return value to your deep function, and then loop to determine this value

const deep = arr => {
    for (let i = 0; i < arr.length; iPP) {
        if (Array.isArray(arr[i])) {
            if(deep(arr[i]))
                return true
        } else {
            console.log(arr[i])
            if (arr[i] === 2) return true
        }
    }
    return false
}

there is no situation in which recursion cannot jump out. All recursions can be changed into loops, and all can jump out

.
Menu