Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How do I compare an array value in an if statement

New Here ,
Jul 29, 2013 Jul 29, 2013

 

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

                 

}

TOPICS
ActionScript
722
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 29, 2013 Jul 29, 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

Translate
LEGEND ,
Jul 29, 2013 Jul 29, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 29, 2013 Jul 29, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 29, 2013 Jul 29, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 29, 2013 Jul 29, 2013

Yes, that was the main part of the answer. The other should have never happened. The person I was trying to help, actually changed the variable. Thankfully I asked them to read me what they had in the first if statement. Once the correct variable (cdlAnswer[index]) was put back and tested (did not work), I had them add the quotes you suggested and everything started working for them. It's a bummer trying to work with someone over the phone. Thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 30, 2013 Jul 30, 2013
LATEST

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines