【问题标题】:How does variable decrement work in recursive functions变量递减如何在递归函数中工作
【发布时间】:2021-07-22 05:12:30
【问题描述】:

我知道这是一个非常初级的问题 这是场景

Let number = 5; 
number - 1;
console.log(number);

显示5,为什么不显示4;

【问题讨论】:

  • number - 1number 中减去 1,但没有对结果进行任何处理。
  • 您必须将值分配给您的变量。 number = number - 1;
  • 我建议不要对您的问题进行重大更改。这个问题从一个简单的变量赋值开始,变成了一个关于递归的问题。这使得之前的所有答案看起来都有些不完整。您可以打开多个问题并链接到旧问题。
  • @Nickofthyme 好的,我将添加一个单独的问题

标签: javascript web recursion javascript-objects development-environment


【解决方案1】:

您从未更改过变量的值。您可以像这样分配递减的值:

let number = 5; 
number = number - 1;
console.log(number);

decrement operator也可以使用。

let number = 5; 
--number;
console.log(number);

let number = 5; 
number--;
console.log(number);

【讨论】:

    【解决方案2】:

    如果您想查看或使用操作的更改,请始终在新变量或同一变量中分配值。 如果将它分配给一个新变量,那么它将变为:

    Let number = 5; 
    let newNumber = number - 1;
    console.log(number); -- output 5
    console.log(newNumber); --output 4
    

    如果你把它分配给同一个变量,它会变成

    Let number = 5; 
    number = number - 1;
    console.log(number); -- output 4
    

    【讨论】:

      【解决方案3】:

      数字 -1 确实是 4。但数字仍然是 5。 你需要写

      number = number -1;
      

      准确无误

      【讨论】:

        【解决方案4】:

        您正在递减而不分配给您的变量number - 1;。相反,有两种方法可以减少您的情况,

        案例一:

        数字 = 数字 - 1;

        案例 2:

        数字--;

        两者都可以正常工作。

        【讨论】:

          【解决方案5】:

          像计算机一样思考。

          let number = 5; 
          number - 1;
          console.log(number);
          

          JavaScript 会将第二行中的number 替换为number 的最新变量赋值,因此基本上它会变成...

          let number = 5; 
          5 - 1; // this does no assignment so basically useless
          console.log(5);
          

          但是,如果您要重新分配变量 number,它将看起来像这样...

          let number = 5; 
          number = number - 1; // or number--
          console.log(number);
          
          // you can think of this like...
          let number = 5; 
          number = 5 - 1; // number is now 4
          console.log(4);
          

          更新

          使用新功能,您添加到这个问题变得有点复杂,因为您现在处理的是recursion。就好像我们是“计算机”(又名调试)一样单步执行...

          // actual function
          function repeatStringNumTimes(string, times) {
            if (times < 0) 
              return "";
            if (times === 1) 
              return string;
            else 
              return string + repeatStringNumTimes(string, times - 1);
          }
          repeatStringNumTimes("abc", 3);
          
          // Walking through this function from the first invocation above
          
          // first function call
          function repeatStringNumTimes(string, times) { // string = 'abc' and times = 3
            if (3 < 0) // false - thus skipped
              return "";
            if (3 === 1) // false - thus skipped
              return 'abc';
            else // run because all others were false/skipped
              // Note the code below will not return until the repeatStringNumTimes below returns
              return 'abc' + repeatStringNumTimes('abc', 3 - 1);
          }
          
          // second function call - first recursive call
          function repeatStringNumTimes(string, times) { // string = 'abc' and times = 2 (i.e. 3 - 1)
            if (2 < 0) // false - thus skipped
              return "";
            if (2 === 1) // false - thus skipped
              return 'abc';
            else // run because all others were false/skipped
              // Note the code below will not return until the repeatStringNumTimes below returns
              return 'abc' + repeatStringNumTimes('abc', 2 - 1);
          }
          
          // third function call - second recursive call
          function repeatStringNumTimes(string, times) { // string = 'abc' and times = 1 (i.e. 2 - 1)
            if (1 < 0) // false - thus skipped
              return "";
            if (1 === 1) // true - thus executed
              // This block is what is known as a "break case" see below
              return 'abc'; // returns 'abc' end of recursion!
            else // skipped because previous if statement returned 'abc'
              return 'abc' + repeatStringNumTimes('abc', 1 - 1);
          }
          
          // going back to the second function call, which we simplified to
          function repeatStringNumTimes(string, times) { // string = 'abc' and times = 2
            return 'abc' + repeatStringNumTimes('abc', 2 - 1);
          }
          
          // but we know that the return value of `repeatStringNumTimes('abc', 2 - 1)` is just 'abc', thus we can say
          function repeatStringNumTimes(string, times) { // string = 'abc' and times = 2
            return 'abc' + 'abc'; // or 'abcabc'
          }
          
          // going back to the first function call, which we simplified to
          function repeatStringNumTimes(string, times) { // string = 'abc' and times = 3
            return 'abc' + repeatStringNumTimes('abc', 3 - 1);
          }
          
          // but we know that the return value of `repeatStringNumTimes('abc', 3 - 1)` is just 'abcabc', thus we can say
          function repeatStringNumTimes(string, times) { // string = 'abc' and times = 3
            return 'abc' + 'abcabc'; // or 'abcabcabc'
          }
          
          // so the final value of this statement is 'abcabcabc' or...
          repeatStringNumTimes("abc", 3); // returns 'abcabcabc'
          

          什么是break case?中断情况用于结束递归。想象一下你的家谱,假设你想找到你最亲近的父母(伟大的 ^ x 祖母/爸爸)亲戚,名叫 Jane/John。首先你看看你的父母,然后你看看你父母的父母等等,直到你罚款简/约翰。因此,在示例中,我们的中断案例是姓名为 Jane/John。如果没有中断情况(或其他中断),您将导致无限循环。

          请注意,第一个 if 语句 if (times &lt; 0) 从未执行过,这仅仅是因为首先触发了另一个中断情况(即 if (times === 1)),从而结束了递归。

          这是一个非常复杂的想法,仅从文本示例中解释/理解,并恳请您查看此示例以更好地理解递归。 https://www.freecodecamp.org/news/recursion-is-not-hard-858a48830d83/

          【讨论】:

          • 我添加了对您发布的新递归代码的说明。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-18
          • 2012-02-24
          • 1970-01-01
          • 2019-03-05
          • 2023-02-20
          • 2014-04-25
          相关资源
          最近更新 更多