Lieutenant Dan
Lieutenant Dan

Reputation: 8284

keeping track of a series of simple multiple choice web form answers

This is the code I'm trying to use, which seems logical. But doesn't seem to be working.

MyAsFileName.prototype.getTotalScore = function() {
 var totalScore = 0;
 for (var i = 0; i < allQuestions.length; i++) {
  totalScore += allQuestions[i].getCalculatedScore();
  if (currentModule.allQuestions[i].parent.questionCorrect == true) {
   knowledgePoints++;
  } else {
   knowledgePoints--;
  }
 }
 debugLog("Total score: " + totalScore);
 debugLog(knowledgePoints);
 return totalScore;
}

I have allQuestions defined as below:

var allQuestions    = Array(); 

I have knowledgePoints defined as:

 this.knowledgePoints = 10;

I have questionCorrect defined as:

this.questionCorrect = false;

Second fresh attempt made with new class as answer below suggested (commented out for now until I figure out how to get working):

// package
// {
/*public class Quiz {
 //public
 var knowledgePoints: int = 10;
 //public
 var allQuestions: Array = new Array;
 //public
 var questionCorrect: Boolean = false;

 //public
 function getTotalScore(): int {
  var totalScore: int = 0; 

  for (var i = 0; i < allQuestions.length; i++) {
   totalScore += allQuestions[i].getCalculatedScore();

   if (currentModule.allQuestions[i].parent.questionCorrect) {
    knowledgePoints++;
   } else {
    knowledgePoints--;
   }
  }
  debugLog("Total score: " + totalScore);
  debugLog(knowledgePoints);

  return totalScore;
 }
}*/
//}

This code above outputs two errors in flash console:

Error 1. Attribute used outside of class.

Error 2. 'Int' could not be loaded.

Upvotes: 0

Views: 55

Answers (1)

Organis
Organis

Reputation: 7316

It's a weird (and actually non-AS3 way) way to do this. Instead of creating a unnamed closure which refers weird variables from who-knows where, you should make it a normal AS3 class, something like that (in a file named Quiz.as):

package
{
    public class Quiz
    {
        public var knowledgePoints:int = 10;
        public var allQuestions:Array = new Array;
        public var questionCorrect:Boolean = false;

        public function getTotalScore():int
        {
            var totalScore:int = 0;

            // Your code does not explain how you will that Array.
            // It is initially an empty Array of length 0.
            for (var i = 0; i < allQuestions.length; i++)
            {
                totalScore += allQuestions[i].getCalculatedScore();

                if (currentModule.allQuestions[i].parent.questionCorrect)
                {
                    knowledgePoints++;
                }
                else
                {
                    knowledgePoints--;
                }
            }

            // Not sure what it is.
            debugLog("Total score: " + totalScore);
            debugLog(knowledgePoints);

            return totalScore;
        }
    }
}

Upvotes: 1

Related Questions