作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我陷入了嵌套循环。this.isRyt
是一个变量,用于存储从 JSON 检索的字符串。i
是字符串类型的用户输入变量。this.storeArray[]
是一个数组,仅当用户输入的 i
与 this.isRyt
中存储的字符串匹配时才会存储该数组所以基本上我想通过使用索引k
将存储在this.storeArray[]
中的字符串与存储在this.isRryt
中的字符串进行比较(因为用户的多个 i
输入将存储在 this.storeArray[]
中的不同索引位置),如果字符串不匹配,则有一个变量 counter
会递增。incCounter
只是一个用值 0 初始化的简单计数器变量。
我的尝试:我尝试使用下面的循环,但是 this.counter++
在一次内多次递增(k
的多次迭代),因为它位于for 循环。我想让它只增加一次,但 for 条件不应被省略。
filterAnswer(i:any) //Comparing Answer submitted by user with JSON answer
{
this.isRyt = this.questArrayNew1[0].isRight;
if(this.isRyt == i )
{
for(let k = 0 ; k < this.questArray.length ; k++)
{
if(this.storeArray[k] == i)
{
console.log(k);
}
else
{
this.counter++; //WANT TO INCREMENT ONLY ONE TIME IF IT DOESNT SATISFY THE CONDITION FOR WHOLE K=0,1,2,3.. variable
}
}
this.storeArray[this.incCounter] = i ;
console.log(this.storeArray);
this.incCounter++;
}
else
{
return 0;
}
}
最佳答案
如果我理解正确的话, this.counter 只需要增加一次。你可以尝试这样的事情:
filterAnswer(i:any) //Comparing Answer submitted by user with JSON answer
{
var notCounted = true; //condition for this.counter++ code block to be executed
this.isRyt = this.questArrayNew1[0].isRight;
if(this.isRyt == i )
{
for(let k = 0 ; k < this.questArray.length ; k++)
{
if(this.storeArray[k] == i)
{
console.log(k);
}
else
{
while(notCounted)
{ //executes while bool is true
this.counter++;
notCounted = false; //incremented so now no longer needed
}
}
}
this.storeArray[this.incCounter] = i ;
console.log(this.storeArray);
this.incCounter++;
}
else
{
return 0;
}
}
关于javascript - 嵌套 IF-FOR-IF 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50024808/
我是一名优秀的程序员,十分优秀!