我陷入了嵌套循环。
this.isRyt
是一个变量,其中存储了来自JSON的重试字符串。
i
是string类型的用户输入变量。
this.storeArray[]
是一个数组,只有当它与i
变量中存储的字符串匹配时,才会存储来自用户的this.isRyt
的每个输入。所以基本上我想比较{{1}中存储的字符串通过对this.storeArray[]
中存储的字符串使用索引k
(因为来自用户的多个this.isRryt
输入将存储在i
中的不同索引位置),如果字符串是不匹配,然后变量this.storeArray[]
会增加。counter
只是一个用值0初始化的简单计数器变量。
我的尝试:我尝试使用下面的循环,但是incCounter
在for循环中一次多次递增(this.counter++
的多次迭代)。我想让它只增加一次,但 for 条件不应该被省略。
k
答案 0 :(得分:1)
如果我正确理解你,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;
}
}