Javascript 区分循环中的两个不同操作
我正试着做这样的事情Javascript 区分循环中的两个不同操作,javascript,if-statement,modulo,Javascript,If Statement,Modulo,我正试着做这样的事情 var teller = 1; if (teller % 2 === 0) { "do this" } else { "do something else" } teller++; 问题是他总是进入else循环。 有人知道为什么?因为1%2==0返回false 你可能想把它放在一个循环中 var teller = 1; while(teller < 100){ //100 is the range, just a sample if (teller
var teller = 1;
if (teller % 2 === 0) {
"do this"
} else {
"do something else"
}
teller++;
问题是他总是进入else循环。
有人知道为什么?因为
1%2==0
返回false
你可能想把它放在一个循环中
var teller = 1;
while(teller < 100){ //100 is the range, just a sample
if (teller % 2 === 0) {
"do this"
} else {
"do something else"
}
teller++;
}
var-teller=1;
而(teller<100){//100是范围,只是一个样本
如果(出纳员%2==0){
“这样做”
}否则{
“做点别的”
}
teller++;
}
因为1%2==0
返回false
你可能想把它放在一个循环中
var teller = 1;
while(teller < 100){ //100 is the range, just a sample
if (teller % 2 === 0) {
"do this"
} else {
"do something else"
}
teller++;
}
var-teller=1;
而(teller<100){//100是范围,只是一个样本
如果(出纳员%2==0){
“这样做”
}否则{
“做点别的”
}
teller++;
}
因为1%2==0
返回false
你可能想把它放在一个循环中
var teller = 1;
while(teller < 100){ //100 is the range, just a sample
if (teller % 2 === 0) {
"do this"
} else {
"do something else"
}
teller++;
}
var-teller=1;
而(teller<100){//100是范围,只是一个样本
如果(出纳员%2==0){
“这样做”
}否则{
“做点别的”
}
teller++;
}
因为1%2==0
返回false
你可能想把它放在一个循环中
var teller = 1;
while(teller < 100){ //100 is the range, just a sample
if (teller % 2 === 0) {
"do this"
} else {
"do something else"
}
teller++;
}
var-teller=1;
而(teller<100){//100是范围,只是一个样本
如果(出纳员%2==0){
“这样做”
}否则{
“做点别的”
}
teller++;
}
逐步浏览您的代码:
var teller = 1; //teller is now 1
if (teller % 2 === 0) { //1 % 2 === 1, so this statement is skipped
"do this"
} else { //since the if statement was skipped, this gets run
"do something else"
}
teller++; //this has no affect on the above
如果要将其放入循环中,请参阅@DryrandzFamador的答案。逐步浏览代码:
var teller = 1; //teller is now 1
if (teller % 2 === 0) { //1 % 2 === 1, so this statement is skipped
"do this"
} else { //since the if statement was skipped, this gets run
"do something else"
}
teller++; //this has no affect on the above
如果要将其放入循环中,请参阅@DryrandzFamador的答案。逐步浏览代码:
var teller = 1; //teller is now 1
if (teller % 2 === 0) { //1 % 2 === 1, so this statement is skipped
"do this"
} else { //since the if statement was skipped, this gets run
"do something else"
}
teller++; //this has no affect on the above
如果要将其放入循环中,请参阅@DryrandzFamador的答案。逐步浏览代码:
var teller = 1; //teller is now 1
if (teller % 2 === 0) { //1 % 2 === 1, so this statement is skipped
"do this"
} else { //since the if statement was skipped, this gets run
"do something else"
}
teller++; //this has no affect on the above
如果您想将其放入循环中,请参阅@DryrandzFamador的答案。您的问题标题中提到的循环…在哪里????您是否尝试过teller%2==0,因此使用two==代替==?“else”不是循环,而是条件的一部分。“循环”是带有“while”或“for”的词。我找到了答案。问题是在onclick事件中声明了“teller”。这意味着单击时,值始终变为1。问题标题中提到的循环在哪里…???您是否尝试过出纳员%2==0,因此使用两个==而不是==?“else”不是循环,它是条件的一部分。“循环”是带有“while”或“for”的词。我找到了答案。问题是在onclick事件中声明了“teller”。这意味着单击时,值始终变为1。问题标题中提到的循环在哪里…???您是否尝试过出纳员%2==0,因此使用两个==而不是==?“else”不是循环,它是条件的一部分。“循环”是带有“while”或“for”的词。我找到了答案。问题是在onclick事件中声明了“teller”。这意味着单击时,值始终变为1。问题标题中提到的循环在哪里…???您是否尝试过出纳员%2==0,因此使用两个==而不是==?“else”不是循环,它是条件的一部分。“循环”是带有“while”或“for”的词。我找到了答案。问题是在onclick事件中声明了“teller”。这意味着单击时,该值始终变为1。