【问题标题】:I need to create an infinite loop我需要创建一个无限循环
【发布时间】:2018-11-04 14:31:03
【问题描述】:

出于我的目的,我需要创建一个循环,其中一个变量以这种方式循环: 0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2, 3, 4, 3, 2, 1...

它看起来很简单,但一个多小时我想知道该怎么做。 我的目的是这样移动星星

*....
.*...
..*..
...*.
....*
...*.
..*..
.*...
*....
*....
.*...
..*..
...*.
....*

【问题讨论】:

  • 那只会让你的浏览器崩溃:-(
  • 您需要打印吗?什么是停止点?目的是什么?
  • 你想要0,1,2,3,4,3,2,1,0,1...(根据你的文字)还是0,1,2,3,4,3,2,1,0,0,1...(根据ascii-art)?

标签: javascript loops infinity


【解决方案1】:

将该循环编写为生成器 (function *... yield),然后在需要时使用它 (for...of)。当然,消费代码必须提供一些终止条件。

function* bounce(min, max) {
  while (1) {
    for (let i = min; i < max; i++)
      yield i;
    for (let i = max; i > min; i--)
      yield i;
  }
}

STEPS = 10

for(let x of bounce(0, 4)) {
  console.log(x)
  if (--STEPS === 0) break;
}

【讨论】:

    【解决方案2】:

    您可以使用以下代码生成您需要的数字模式。但是,您将无法无限运行它,因为它会使浏览器崩溃。

    如果你想测试,我已经添加了使循环无限的说明。

    根据您的要求,rep 变量的值越大就足够了。

    let min = 0; // Start/min value
    let max = 4; // Max value
    let dir = 1; // Count direction (+1/-1)
    
    let counter = min; // Your counter variable
    
    let rep = 24; // Remove this line and change condition inside while to true for infinite loop
    
    do {
      console.log(counter);
    
      dir = counter===max?-1:counter===min?1:dir;
      counter+=dir;
    } while (rep-->0); // Change this expression to true for infinite loop

    【讨论】:

      【解决方案3】:

      您可以使用 setTimeout 或 setInterval 来执行此操作:

      let number = 0;
      let increment = 1;
      const from = 0;
      const to = 4;
      
      const starDiv = document.getElementById("star");
      
      function printStar(number) {
        const text = [0, 1, 2, 3, 4].map(
          i => (i === number) ? '*' : '-'
        ).join('');
        starDiv.innerText = text;
      }
      
      function loop() {
        printStar(number);
        number += increment;
      
        if (number == to) {
          increment = -1;
        } else if (number == from) {
          increment = 1;
        }
      }
      
      const time = 10; // 10 millisecond between step
      setInterval(loop, time);
      <div id="star">
      </div>

      【讨论】:

        【解决方案4】:

        您可以有一个简单的计数器,然后使用modulo 8 来获取迭代。

        let x = (i += direction) % 8;
        let y = x > 4 ? 8 - x : x;
        

        这个例子甚至打印了 ascii 艺术;)

        let i = -1;
        let direction = +1;
        const out = [
          "*....",
          ".*...",
          "..*..",
          "...*.",
          "....*",
          "...*.",
          "..*..",
          ".*...",
          "*....",
        ];
        setInterval(function() {
          let x = (i += direction) % 8;
          let y = x > 4 ? 8 - x : x;
          window.document.write(y + " " + out[x] + "<br>");
        }, 1000);

        【讨论】:

          【解决方案5】:
          (function(min,max,max_loops)
          {
              for(var loop = 1; loop <= max_loops; loop++, [min,max] = [-max,-min])
              {
                  for(num = min; num < max + (loop == max_loops); num++)
                  {
                      console.log(".".repeat(Math.abs(num)) + "*" +  ".".repeat(Math.max(Math.abs(max),Math.abs(min)) - Math.abs(num)))
                  }
              }
          })(0,4,3)
          

          但是既然你需要一个无限循环,使用生成器应该更合适。

          【讨论】:

            猜你喜欢
            • 2015-01-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-02-20
            相关资源
            最近更新 更多