分享
三行代码  ›  专栏  ›  技术社区  ›  Manan

需要javascript帮助。特别是数值和数字

  •  0
  • Manan  · 技术社区  · 1 周前

    基本上,我想在javascrpit上做草。我想压缩代码,因为我一直在“moveTo”标签的数字上加10。有没有办法浓缩这个?我只想要短的代码,而不复制和粘贴代码一遍又一遍,只是增加10。例如,不要复制moveTo(0355),然后复制并粘贴代码,只需将moveTo更改为(0365)。我想保持简洁。我能做这个吗?

    function drawBackground() {
      penColor("lightblue");
      dot(1000);
      moveTo(0, 0);
      penColor("yellow");
      dot(100);
      penUp();
      moveTo(100, 300);
    }
    drawBackground();
    function drawGrass() {
      penWidth(7);
      turnTo(22);
      penColor("green");
      penDown();
      moveForward(25);
      turnRight(135);
      moveForward(25);
      turnLeft(135);
    }
    
    penUp();
    moveTo(0, 355);
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    penUp();
    moveTo(0, 365);
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    penUp();
    moveTo(0, 375);
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    penUp();
    moveTo(0, 385);
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    penUp();
    moveTo(0, 395);
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    penUp();
    moveTo(0, 405);
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    penUp();
    moveTo(0, 415);
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    penUp();
    moveTo(0, 425);
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    penUp();
    moveTo(0, 425);
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    penUp();
    moveTo(0, 425);
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    penUp();
    moveTo(0, 435);
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    penUp();
    moveTo(0, 445);
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    penUp();
    moveTo(0, 455);
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    penUp();
    moveTo(0, 465);
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    drawGrass();
    
    1 回复  |  直到 1 周前
        1
  •  2
  •   jacob13smith    1 周前

    使用循环来减少重复。循环你想增加点10次的次数,然后循环每一次画17次草(就像你在你的例子中一样),然后更新你的计数器并移动到下一个10。

    你可以在 function drawGrass(){} 声明如下:

    var counter = 355;
    for (var i = 0; i < TEN_INTERVALS; i++){
       penUp();
       for (var x =0; x < 17; x++){
          drawGrass();
       }
       counter += 10;
       moveTo(0, counter);
    }
    

    您的代码如下所示:

    function drawBackground() {
      penColor("lightblue");
      dot(1000);
      moveTo(0, 0);
      penColor("yellow");
      dot(100);
      penUp();
      moveTo(100, 300);
    }
    drawBackground();
    function drawGrass() {
      penWidth(7);
      turnTo(22);
      penColor("green");
      penDown();
      moveForward(25);
      turnRight(135);
      moveForward(25);
      turnLeft(135);
    }
    var counter = 355;
    for (var i = 0; i < TEN_INTERVALS; i++){
       penUp();
       for (var x =0; x < 17; x++){
          drawGrass();
       }
       counter += 10;
       moveTo(0, counter);
    }
    

    你只需要更换 TEN_INTERVALS 你想增加10次的次数(在你的例子中 13 ).