这段代码中 current 和 history 的值是多少.请解释一下 [英] what is the value of current and history in this code. please explain this

查看:28
本文介绍了这段代码中 current 和 history 的值是多少.请解释一下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function findSolution(target) {//我们没有提到 current 和 history 的值,那怎么可能取当前和历史的价值?函数查找(当前,历史){如果(当前 == 目标){返回历史;} else if (current > target) {返回空;} 别的 {return find(current + 5, `(${history} + 5)`) ||find(current * 3, `(${history} * 3)`);}}返回查找(1,1");}控制台.log(findSolution(24));//→ (((1 * 3) + 5) * 3)

//请解释结果

解决方案

参数名称用于创建变量.这些值是在调用函数时定义的.

<块引用>

function find(current, history) {返回查找(1,1");

current1 并且 history1"(并且它们将具有不同的值在程序的其他部分使用不同的参数调用).

function findSolution(target) {

// we didn't mentioned values of current and history then how can it
   takes the values of current and history?

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

console.log(findSolution(24));
// → (((1 * 3) + 5) * 3)

// please explain the result

解决方案

Argument names are used to create variables. The values are defined when the function is called.

function find(current, history) {

return find(1, "1");

current is 1 and history is "1" (and they will have different values when they are called with different parameters in other parts of the program).

这篇关于这段代码中 current 和 history 的值是多少.请解释一下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆