Неправильное поведение с условиями и операторами - PullRequest
0 голосов
/ 26 февраля 2012

У меня есть следующая итерация:



    The content of the array respuesta is: Africa, Europa, Norteamerica
    The content of the array resultado is: Incorrect, Correct, Incorrect

    I created a Array to include both of them: 

    var contPre:Array = [ this.respuesta, this.resultado ];



    for (var a:uint = 0; a < contPre[0].length; a++){

     if (this.radioGroup1.selection.value == contPre[0][a] && 
          contPre[1][a] == "Correcto") {

        result_txt.text = "Correct";
        valor = 1;

      } else {

        result_txt.text = "Incorrect";
        valor = 0;

        }

      }


     The first time that I've executed this I found this:

     this.radioGroup1.selection.value   Obtained value: Europa
     contPre[0][a]:                     Obtained value: Europa 
     contPre[1][a]:                     Obtained value: Correcto

     The sentence go out for the second option "Incorrect".


     Somebody can explain why is this happening?


1 Ответ

0 голосов
/ 26 февраля 2012

Проблема в том, что когда вы находите правильный ответ, вы не выходите из цикла. Так что поменяйте:

if (this.radioGroup1.selection.value == contPre[0][a] && contPre[1][a] == "Correcto") {
    result_txt.text = "Correct";
    valor = 1;
}

в

if (this.radioGroup1.selection.value == contPre[0][a] && contPre[1][a] == "Correcto") {
    result_txt.text = "Correct";
    valor = 1;
    break; // Add this line
}
...