【问题标题】:While loop and increment in JavaScriptJavaScript中的while循环和增量
【发布时间】:2018-06-26 06:32:40
【问题描述】:

在下面的函数中,我需要增加 years 变量,以便找到在达到所需利润之前需要经过的年数。我注意到如果我使用years++ 而不是++years,此功能将不起作用。我理解这两种增量方法之间的区别,但是,我仍然不明白,而在这种特殊情况下,years++ 导致循环只执行一次。

 function calculateYears(investment, interestRate, tax, desiredProfit) {
        var years = 0;
        while(investment < desiredProfit && ++years){
          investment += (investment * interestRate) * (1 - tax);
        }
    
        return years;
    }
    var years = calculateYears(1000, 0.05, 0.18, 1100);
    console.log(years);

【问题讨论】:

    标签: javascript while-loop increment


    【解决方案1】:

    它只执行一次,因为用于检查真实性的years 的值是0,即在递增之前。

    MDN Documentation

    【讨论】:

      【解决方案2】:

      在这种特殊情况下,我仍然不明白 years++ 的原因 循环只执行一次。

      因为&amp;&amp; years++ 转换为&amp;&amp; 0 将转换为虚假值

      如果要使用years++,请将years初始化为1

      function calculateYears(investment, interestRate, tax, desiredProfit) {
          var years = 1;
          while(investment < desiredProfit && years++){
            investment += (investment * interestRate) * (1 - tax);
          }
      
          return years - 1;
      }
      console.log(calculateYears(1000, 0.05, 0.18, 1100));

      【讨论】:

      • 它返回 4 而不是 3。看我的回答
      • @MaxMaximilian 感谢您的指出,在检查最终while 时会有额外的增量,我已经进行了更新。
      • @gurvinder372 你的解释非常清楚和有用。谢谢!我在更改 var years = 1 时进行了测试。它给出了有趣的结果 - while 循环有效,非常完美。但它没有给出所需的逻辑——因为第一个循环中的投资价值需要被视为第一年过去之前的价值。总之,为了让函数给出正确的结果,我需要用 0 对其进行初始化。但是有了你的解释以及其他人的回答,我现在可以理解为什么事情会以它们的工作方式工作。非常感谢!
      • 我认为,这种方式不是很干净。这行得通,这很好,但是 1)它在循环之后进行了不必要的计算,2)在每个循环上它检查 &amp;&amp; years++ 这也会产生不必要的计算。作为程序员,我们应该让代码更简洁,因为这可能会出现在更大的项目中,在这些项目中,您需要创建在速度等方面表现更好的东西。但是,是的,这解释了问题的根源,所以很好的回答 gurvinder372
      • @MaxMaximilian 我同意这不是最干净的,并且可以有更多可读的方式来做到这一点。
      【解决方案3】:

      你应该在循环中使用years++

      function calculateYears(investment, interestRate, tax, desiredProfit) {
          var years = 0;
          while(investment < desiredProfit){
            investment += (investment * interestRate) * (1 - tax);
            years++;
          }
      
          return years;
      }
      alert(calculateYears(1000, 0.05, 0.18, 1100));
      

      JSFIDDLE

      【讨论】:

        【解决方案4】:
        while(investment < desiredProfit && ++years)
        

        这个条件不会执行,因为 years 是 0 并且它在 1 循环之后递增,所以它就像

        desiredProfit && 0 -> false/0
        

        之后

        investment < 0 //is always false
        

        【讨论】:

          【解决方案5】:

          您确实忘记了您的 while 循环没有增加年份的值

           while(investment < desiredProfit && ++years){
            investment += (investment * interestRate) * (1 - tax); years++;
          }
          

          【讨论】:

            【解决方案6】:

            因为&amp;&amp; years++ 转换为&amp;&amp; 0,而&amp;&amp; 0 将转换为假值。

            如果要使用years++,请将年份初始化为 1。

            【讨论】:

              【解决方案7】:

              为什么不做这样的事情

              let i = -6;
              let end = 9;
              while ((i++ || true) && i < end) {
              ...do something...
              }
              

              【讨论】:

                猜你喜欢
                • 2022-01-18
                • 1970-01-01
                • 2011-11-07
                • 1970-01-01
                • 2016-04-22
                • 2015-10-10
                • 2015-02-12
                • 2023-03-14
                • 1970-01-01
                相关资源
                最近更新 更多