嵌套的IF-FOR-IF循环

时间:2018-04-25 14:26:31

标签: javascript

我陷入了嵌套循环。 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

1 个答案:

答案 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;
     }

  }