对for-loop

时间:2017-09-06 19:44:43

标签: javascript loops for-loop

我有这个循环,每2分钟计算增加温度10度。 当达到16分钟及以上时,我需要将温度固定在120度 我尝试了很多解决方案,但没有一个工作? 任何建议???

var temp = 30,
    min = 0;
    console.log("Cooking started at " + temp + " deg");

for (min = 0; min <= 25; min++) {

   if (min % 2 === 0) {
       temp += 10;
       console.log("Minutes passed: " + min + 
                    ", we are cooking at " + temp + " deg");
   }

   if (min % 2 !== 0 && min === 25) {
       console.log("Total minutes of cooking is 25 min");
   }
}

输出应该是这样的:

Cooking started at 30 deg
Minutes passed: 0, we are cooking at 40 deg
Minutes passed: 2, we are cooking at 50 deg
Minutes passed: 4, we are cooking at 60 deg
Minutes passed: 6, we are cooking at 70 deg
Minutes passed: 8, we are cooking at 80 deg
Minutes passed: 10, we are cooking at 90 deg
Minutes passed: 12, we are cooking at 100 deg
Minutes passed: 14, we are cooking at 110 deg
Minutes passed: 16, we are cooking at 120 deg
Minutes passed: 18, we are cooking at 120 deg
Minutes passed: 20, we are cooking at 120 deg
Minutes passed: 22, we are cooking at 120 deg
Minutes passed: 24, we are cooking at 120 deg
Total minutes of cooking is 25 min

6 个答案:

答案 0 :(得分:3)

在达到已知的最大值后,你想要保持循环似乎很奇怪。最简单的方法是将最大循环次数设置为<=16,因为您知道需要在那里结束。

var temp = 30,
    min = 0;
    console.log("Cooking started at " + temp + " deg");

for (min = 0; min <= 16; min++) {

   if (min % 2 === 0) {
       temp += 10;
       console.log("Minutes passed: " + min + 
                    ", we are cooking at " + temp + " deg");
   }

   if (min % 2 !== 0 && min === 16) {
       console.log("Total minutes of cooking is 25 min");
   }
}

或者,如果你必须持续25分钟,你只需要包括16分钟的测试并相应地设置temp

var temp = 30,
    min = 0;
    console.log("Cooking started at " + temp + " deg");

for (min = 0; min <= 25; min++) {

   if(min >= 16) {
      // If we've arrived in this branch, at least 16 minutes of cooking has already happened
      temp = 120;  // Set temp to max
      console.log("Minutes passed: " + min + ", we are cooking at " + temp + " deg");
   } else if (min % 2 === 0) {
       // If we've arrived in this branch, we must be less than 16 minutes in and 
       // we are on an even minute, so proceed as normal
       temp += 10;
       console.log("Minutes passed: " + min + ", we are cooking at " + temp + " deg");
   }

   if (min % 2 !== 0 && min === 25) {
       console.log("Total minutes of cooking is 25 min");
   }
}

答案 1 :(得分:3)

如果您只是根据temp应用min的公式,则可以大大减少代码。使用Math.min可确保温度不会超过某个值。因为你每两分钟只打印一次,所以你可以让你的循环花费很多分钟。最后,由于25分钟的消息只发生一次,最后一次,最好放在循环之外:

console.log("Cooking started at 30 deg");
for (var min = 0; min <= 25; min+=2) {
   var temp = Math.min(120, 40 + min*5);
   console.log("Minutes passed: " + min + ", we are cooking at " + temp + " deg");
}
console.log("Total minutes of cooking is 25 min");

答案 2 :(得分:2)

只需设置检查时间是否大于16分钟的条件,然后将temp设置为120.否则,将其递增5:

var temp = 30,
min = 0;
console.log("Cooking started at " + temp + " deg");

for (min = 0; min <= 25; min++) {

   if (min >= 16) {
       temp = 120;
   } else {
       temp += 5;
   }

   if (min % 2 === 0) {
       console.log("Minutes passed: " + min + 
                    ", we are cooking at " + temp + " deg");
   }

   if (min % 2 !== 0 && min === 25) {
       console.log("Total minutes of cooking is 25 min");
   }
}

此外,最好将日志记录和温度设置保存在单独的代码块中,以免事情变得太混乱。

答案 3 :(得分:1)

如果我正确地阅读了您的问题,您希望在值{120}后停止修改temp。您有两个选项:break;continue;

break;将完全停止for循环,并且在您调用它之后它将停止循环。

continue;将跳过该迭代的其余循环。如果你想在120点后保留循环的其余部分,这就是你想要的那个。示例:

var temp = 30,
min = 0;
console.log("Cooking started at " + temp + " deg");

for (min = 0; min <= 25; min++) {

  if(temp >= 120) {
    continue;
    // or 'break;' if you're done with the entire loop.
  }

  if (min % 2 === 0) {
      temp += 10;
      console.log("Minutes passed: " + min + 
                ", we are cooking at " + temp + " deg");
  }

  if (min % 2 !== 0 && min === 25) {
      console.log("Total minutes of cooking is 25 min");
  }
}

没有彻底测试过,但你明白了。

答案 4 :(得分:1)

检查增量变量temp后,简单使用中断。

&#13;
&#13;
console.clear();
var temp = 30,
  min = 0;
console.log("Cooking started at " + temp + " deg");

for (min = 0; min <= 25; min++) {
  if (temp >= 120) {
    break;
  }

  if (min % 2 === 0) {
    temp += 10;
    console.log("Minutes passed: " + min +
      ", we are cooking at " + temp + " deg");
  }

  if (min % 2 !== 0 && min === 25) {
    console.log("Total minutes of cooking is 25 min");
  }
}
&#13;
&#13;
&#13;

答案 5 :(得分:1)

您可以在console.log()内设置三元运算符条件,以最小化代码:

var temp = 30,
    min = 0;
    console.log("Cooking started at " + temp + " deg");

for (min = 0; min <= 25; min++) {
   if (min % 2 === 0) {
       temp += 10;
       console.log("Minutes passed: " + min + 
                    ", we are cooking at " + (temp>120?120:temp) + " deg");
   }

   if (min % 2 !== 0 && min === 25) {
       console.log("Total minutes of cooking is 25 min");
   }
}

说明:

(temp>120?120:temp)
// shorthand to
if (temp>120) { 120 } else { temp }