简体   繁体   English

每次循环打印一个数字

[英]Printing For Loop Is One Number Off Each Time

I'm playing around with some HTML and Javascript.我正在玩一些 HTML 和 Javascript。 I'm trying to print out 100 numbers on a screen after I press a button on a webpage.按下网页上的按钮后,我试图在屏幕上打印 100 个数字。 I can get the numbers to print on the screen but when I want to replace a number with a string like "Apple" if the number is divisible by 5. The code is below:我可以将数字打印在屏幕上,但是当我想用“Apple”之类的字符串替换数字时,如果该数字可以被 5 整除。代码如下:

function testFunction() {
  var wrapper = document.getElementById("Test");
  var myHTML = '';

  for (var i = -1; i < 20; i++) {
        if (i % 5 == 0 && i % 3 == 0) {
        myHTML += '<span class="test">Pineapple</span><br/>';
    } else if (i % 5 == 0) {
        myHTML += '<span class="test">Apple</span><br/>';
        } else if (i % 3 == 0) {
          myHTML += '<span class="test">Pine</span><br/>';
        } else {
        myHTML += '<span class="test">' + (i + 1) + '</span><br/>';
    }
  }
  wrapper.innerHTML = myHTML
}

The output is always off by one element so it would print "5" and then the next line would be "Apple" instead of replacing "5" with "Apple" and then printing "6" output 总是关闭一个元素,因此它将打印“5”,然后下一行将是“Apple”,而不是用“Apple”替换“5”,然后打印“6”

Here is the output:这是 output:

0
Pineapple
2
3
Pine
5
Apple
Pine
8
9
Pine
Apple
12
Pine
14
15
Pineapple
17
18
Pine
20

You are printing i+1, instead of i.您正在打印 i+1,而不是 i。

  var wrapper = document.getElementById("Test");
  var myHTML = '';

  for (var i = -1; i < 20; i++) {
        if (i % 5 == 0 && i % 3 == 0) {
        myHTML += '<span class="test">Pineapple</span><br/>';
    } else if (i % 5 == 0) {
        myHTML += '<span class="test">Apple</span><br/>';
        } else if (i % 3 == 0) {
          myHTML += '<span class="test">Pine</span><br/>';
        } else {
        myHTML += '<span class="test">' + (i ) + '</span><br/>';
    }
  }
  wrapper.innerHTML = myHTML
}

The trouble is in the starting point of your for loop, where you initialize with -1 instead of 0. While you use (i + 1) at the end when you print a number, which is why it causes your results to be one number off.问题出在你的 for 循环的起点,你用 -1 而不是 0 进行初始化。当你在最后打印一个数字时使用 (i + 1) 时,这就是为什么它会导致你的结果是一个数字离开。 Use for(var i = 0; i <= 20; i++) instead of for (var i = -1; i < 20; i++) when you want to loop 20 times.当您想要循环 20 次时,请使用for(var i = 0; i <= 20; i++)而不是for (var i = -1; i < 20; i++)

function testFunction() {
  var wrapper = document.getElementById("Test");
  var myHTML = '';

  for (var i = 0; i <= 20; i++) {
        if (i % 5 == 0 && i % 3 == 0) {
        myHTML += '<span class="test">Pineapple</span><br/>';
    } else if (i % 5 == 0) {
        myHTML += '<span class="test">Apple</span><br/>';
        } else if (i % 3 == 0) {
          myHTML += '<span class="test">Pine</span><br/>';
        } else {
        myHTML += '<span class="test">' + i + '</span><br/>';
    }
  }
  wrapper.innerHTML = myHTML
}

Using this code I was able to replace 5 with Apple.使用此代码,我能够用 Apple 替换 5。

 function testFunction() { var wrapper = document.getElementById("Test"); var myHTML = ''; for (var i = -1; i < 20; i++){ if (i % 5 == 0) { myHTML += '<span class="test">Apple</span><br>'; } else{ myHTML += '<span class="test">' + i + '</span><br>' } } wrapper.innerHTML = myHTML } testFunction();
 <div id="Test"></div>

Use this snippet to follow what exactly happens within your loop.使用此代码段来跟踪循环中究竟发生了什么。 It should give you some clues.它应该给你一些线索。

 const log = Logger(); testFunction(); function testFunction() { for (let i = -1; i < 20; i += 1) { if (i % 5 == 0 && i % 3 == 0) { log(`${i} % 5 === 0 && ${i} % 3 === 0 => Pineapple`); } else if (i % 5 == 0) { log(`${i} % 5 === 0 => Apple` ); } else if (i % 3 == 0) { log(`${i} % 3 === 0 => Pine`); } else { log(`i = ${i}, i + 1 = ${i + 1}`); } } } function Logger() { let logEl; if (typeof window === "object") { logEl = document.querySelector("#log") || (() => { document.body.appendChild(Object.assign( document.createElement('pre'), { id: "log" }) ); return document.querySelector("#log"); })(); return (...logLines) => logLines.forEach(s => logEl.textContent += `${s}\n`); } else { return (...logLines) => logLines.forEach(ll => console.log(`* `, ll)); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM