Javascript 嵌套的IF-FOR-IF循环
我被困在嵌套循环中。Javascript 嵌套的IF-FOR-IF循环,javascript,Javascript,我被困在嵌套循环中。 this.isRyt是一个变量,用于存储从JSON检索的字符串。 i是字符串类型的用户输入变量。 this.storeArray[]是一个数组,只有当用户输入的i与存储在this.isRyt变量中的字符串匹配时,才会存储在该数组中。因此,基本上我想使用索引k将存储在this.storeArray[]中的字符串与存储在this.isRryt中的字符串进行比较(由于来自用户的多个i输入将存储在this.storeArray[]中的不同索引位置),如果字符串不匹配,则变量计数器将
this.isRyt
是一个变量,用于存储从JSON检索的字符串。
i
是字符串类型的用户输入变量。
this.storeArray[]
是一个数组,只有当用户输入的i
与存储在this.isRyt
变量中的字符串匹配时,才会存储在该数组中。因此,基本上我想使用索引k
将存储在this.storeArray[]
中的字符串与存储在this.isRryt
中的字符串进行比较(由于来自用户的多个i
输入将存储在this.storeArray[]
中的不同索引位置),如果字符串不匹配,则变量计数器将递增。incCounter
只是一个简单的计数器变量,初始化值为0
我的尝试:我尝试使用下面的循环,但是这个。计数器+++
在一次内多次递增(多次迭代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;
}
}
filterAnswer(i:any)//将用户提交的答案与JSON答案进行比较
{
this.isRyt=this.questArrayNew1[0].isRight;
如果(this.isRyt==i)
{
for(设k=0;k
如果我理解正确,则此.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;
}
}
filterAnswer(i:any)//将用户提交的答案与JSON答案进行比较
{
var notCounted=true;//执行此.counter++代码块的条件
this.isRyt=this.questArrayNew1[0].isRight;
如果(this.isRyt==i)
{
for(设k=0;k 我真的建议你学习控制结构的基本知识,还有JavaScript。如果你不知道如何使用循环和变量,你当然不应该考虑使用类。谢谢阿兰!我一定要研究一下: