Skip to content Skip to sidebar Skip to footer

Why Does This Recursion Execute The Way It Does?

Code snippet is from Eloquent Javascript: function findSolution(target) { function find(current, history) { if (current == target) { return history;

Solution 1:

findSequence(13)
    find(1, "1")
        find(1 + 5, "(1 + 5)") // this becomes null, because both of its nested stacks are null, because both of their nested stacks are null
            find(6 + 5, "((1 + 5) + 5)") // this becomes null, because both of its nested stacks are null
                find(11 + 5, "(((1 + 5) + 5) + 5)"
                    current > target: return null
                find(11 * 3, "(((1 + 5) + 5) * 3)"
                    current > target: return null
            find(6 * 3, "((1 + 5) * 3)") // this becomes null, because both of its nested stacks are null
                find(18 + 5, "(((1 + 5) * 3) + 5)")
                    current > target: return null
                find(18 * 3, "(((1 + 5) * 3) * 3)")
                    current > target: return null
        find(1 * 3, "(1 * 3)") 
            find(3 + 5, "((1 * 3) + 5)")
                find(8 + 5, "(((1 * 3) + 5) + 5)")
                    current == target: return "(((1 * 3) + 5) + 5)"

So, would you say this is accurate ?


Post a Comment for "Why Does This Recursion Execute The Way It Does?"