Skip to main content
Participant
July 29, 2013
Answered

How do I compare an array value in an if statement

  • July 29, 2013
  • 1 reply
  • 798 views

 

When I compare cdlCorrect[index] to 0, it shows true, but when I trace it, it shows 2.

The if statement seems to be comparing the index itself to 0. How do I make it compare the value. In fact, as I keep calling this function (clicking the button), it always compares to 0, but the trace will come back as 2, then 1, then 2, then 0, then 0. I would think I am getting the index, but then it should compare to 0, then 1, then 2 etc. I do not understand why it always compares with zero.

function chkAnswer(mevt:MouseEvent):void {

      cdlDone[index] = 1;

      var rectSize:uint = 300;

      var correctRect:Shape = new Shape();

      correctRect.graphics.beginFill(0x00FF00, 0.5);

     if (cdlCorrect[index] == 0) {

           correctRect.graphics.drawRect(45, 40, rectSize, 30);      //coordinates: 45:x 120:y rectSize:width 50:height

           trace("This is 0");

      }

        if (cdlCorrect[index] == 1) {

               correctRect.graphics.drawRect(45, 80, rectSize, 30);      //coordinates: 45:x 120:y rectSize:width 50:height

                trace("This is 1");

        }

        if (cdlCorrect[index] == 2) {

               correctRect.graphics.drawRect(45, 80, rectSize, 30);      //coordinates: 45:x 120:y rectSize:width 50:height

                trace("This is 2");

        }  

         trace(cdlCorrect[index]);        // 2   

         trace(cdlCorrect);                  //  2,1,2,0,0

                 

}

This topic has been closed for replies.
Correct answer Ned Murphy

If you are reading the values in from an xml file then you either need to convert them into numbers or you need to test them as strings. 

try changing to

if(cdlCorrect[index] == "0"){

   and the same for the others as well

1 reply

Ned Murphy
Legend
July 29, 2013

What is defining the value of 'index'?  Try tracing that value so you know what value of the array is being tested.

Participant
July 30, 2013

index is an int value. it represents the index into the array. If you look at the code, you cans see that the value supposedly in cdlCorrect[index] = 2. The values in cdlCorrect, which represent the correct answer position are read into the array, from a file, via an XML class. Trace cdlCorrect and you see the answer to the 5 questions read into radio buttons. i.e.. 2, then 1, then 2, then 0 and finally 0, meaning that the answer is the 3d radio button, followed by the 2nd radio button, followed by the 3d radio button, followed by the 1st radios button and then the last question's answer is in the 1st radio button.

My question is why does the if statement always show that cdlCorrect[index] = 0, even when the trace(cdlCorrect[index]) shows it as 2?  I am missing something here. Is that trace shows 2, shouldn't the if statement be true when compared to 2 and NOT 0?

I understand that the 2 may possible be an ASCII Hex 32, instead of a binary 0010, but it still should not compare to zero.

Can you explain how the if statement can indicate the value is 0, and the trace statement can indicate the value is 2?

Ned Murphy
Ned MurphyCorrect answer
Legend
July 30, 2013

If you are reading the values in from an xml file then you either need to convert them into numbers or you need to test them as strings. 

try changing to

if(cdlCorrect[index] == "0"){

   and the same for the others as well