2017-02-16 49 views
0

starthistory如何获得一个值?它来自哪里?我正在阅读LINK第二个函数如何获取数据?

如果有人知道请解释一下。

输出

(((1 * 3)+ 5)* 3)

function findSolution(target) { 
 
    function find(start, history) { 
 
    if (start == target) 
 
     return history; 
 
    else if (start > target) 
 
     return null; 
 
    else 
 
     return find(start + 5, "(" + history + " + 5)") || 
 
      find(start * 3, "(" + history + " * 3)"); 
 
    } 
 
    return find(1, "1"); 
 
} 
 

 
console.log(findSolution(24));

+2

从这里'return find(1,“1”);'。当调用findSolution时,内部函数被这个语句调用。 – Tushar

+2

'start'和'history'是'find(start,history){...}'函数的参数。每次调用find()时,都会将值传递给它,例如'find(1,“1”)'。 – nnnnnn

+0

纠正我,如果我错了。 'findSolution'可以返回内部函数?像在代码中一样? – KiRa

回答

2
function findSolution(target) { 
    function find(start, history) {  // <--- NOTICE DECLARATIONS HERE 
     /* SNIP */ 
     return find(start + 5, "(" + history + " + 5)") || 
      find(start * 3, "(" + history + " * 3)"); 
    }  // ^--- FUNCTION CALLS HERE 
    return find(1, "1"); // <--- AND HERE 
} 

console.log(findSolution(24)); 

我已经剪断了一些这个任务无关细节并插入一些评论。正如你所看到的,函数find被声明为starthistory的两个参数。 find首先用1作为start"1"的值作为history的值。在此之后,find函数递归调用它们自身,为这些参数创建新的值。

相关问题