简体   繁体   English

带有if语句的for循环不会多次运行javascript

[英]For loop with if statement will not run more than once javascript

I am trying to print the odd and even numbers from an array but for some reason my loop is being exited after only printing one number ("0 is even" is being printed).我试图从数组中打印奇数和偶数,但由于某种原因,我的循环在仅打印一个数字后退出(“0 是偶数”正在打印)。 I don't understand why it doesn't iterate through 1, 2, 3, 4, 5 and 6 as well?我不明白为什么它也不遍历 1、2、3、4、5 和 6?

<!DOCTYPE html>
<html>
<body>

<p> Click the button to print odd and even numbers </p>

<button onclick="loopNum()">Click me</button>

<p id="loopNumbers"></p>

<script> 
function loopNum(){

var numbers = [1, 2, 3, 4, 5, 6]
var text;

for(var i = 0;i < numbers.length;i++){
        if (i % 2 ==0){
            text = (i += " is even");
            }
        else if (i % 2 !=0){
            text = (i += "is odd");
        }   
        document.getElementById("loopNumbers").innerHTML=text;
    }

}

</script>

</body>
</html>

You are assigning a string to i您正在为i分配一个字符串

i += " is even"

and

i += "is odd"

That breaks the loop.这打破了循环。

You could use this code for even parts您可以将此代码用于偶数部分

text += i + " is even" + '<br>';

Then you need an initialization of text with ''然后你需要用''初始化text

text = '';

and you could omit the second if clause.你可以省略第二个 if 子句。

After all, the output should move a step below, because it should not make an output in every loop, that would overwrite the last content.毕竟,输出应该向下移动一步,因为它不应该在每个循环中进行输出,这会覆盖最后的内容。

 function loopNum() { var numbers = [1, 2, 3, 4, 5, 6], text = ''; for (var i = 0; i < numbers.length; i++) { if (i % 2 == 0) { text += i + " is even<br>"; } else { text += i + " is odd<br>"; } } document.getElementById("loopNumbers").innerHTML = text; }
 <p>Click the button to print odd and even numbers</p> <button onclick="loopNum()">Click me</button> <p id="loopNumbers"></p>

Your += is misplaced.你的+=放错了地方。 Instead of adding to the text variable, you are assigning a string value to i.您不是添加到文本变量,而是将字符串值分配给 i。 Change to this, and it will work.改变这个,它会起作用。

if (i % 2 ==0){
   text += (i + " is even");
}
else if (i % 2 !=0){
   text += (i + " is odd");
}   

First of all, you are reassigning the loop variable i, which is a string after the assignment:首先,您要重新分配循环变量 i,这是分配后的字符串:

i += " some string "

You can not loop with an string variable.您不能使用字符串变量进行循环。

Second issue you have will be that you re-assign the value of your p-element, so in the end you will only see one of you outputs.您遇到的第二个问题是您重新分配了 p 元素的值,因此最终您只会看到其中一个输出。 To get all outputs, you can for example append to the p-element:要获取所有输出,您可以例如附加到 p 元素:

function loopNum(){
var numbers = [1, 2, 3, 4, 5, 6]
var text;
for(var i = 0;i < numbers.length;i++){
  if (i % 2 ==0){
    text = i + " is even";
  }
  else if (i % 2 !=0){
    text = i + "is odd";
  }   
  document.getElementById("loopNumbers").innerHTML += text;
}

} }

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

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