FORUMS: list search recent posts

Help with Array Loop

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
mark2741
Help with Array Loop
on Oct 12, 2006 at 1:58:56 pm

I'm having the hardest time with this....please help as I've about given up : (

I have a 10 question quiz. I have an array in which the answers to the quiz are stored and it is called:

correctAnswers[]

I have a "finish" button in my quiz user interface that, when released, calls a gradeExam() function. When that gradeExam function is called the first thing I want it to do is check to make sure that the user has answered all of the questions and if not then to warn them via a popup window I created.

To store the user's answers to a question I have an array called:

answers[]

So the way it's working now is if a user is on question 1 and selects an answers, then index 0 of the answers array is populated with their answer (the quiz is multiple choice with 4 choices, so the value stored in the array is a number between 1 and 4). So at the end of the quiz, if all the questions are answered, then there is a value in each of the indexes 0 thru 9. If the user skips answering a question then I believe (and the traces confirm this) that the value stored is 'undefined'.

So when the gradeExam() function is fired I am trying to loop through the answers array and look for any values that are undefined. If there are any at all, then I want to show the warning popup. If there are no undefined values, then I want to send them to a frame labelled "results".

I'm apparently not very good with loops : ( I have tried all kinds of variations on this. Here is my latest:

function gradeExam() {
var i:Number = null;
for (x = 0; x < 10; x++) {
if (answers[x] == undefined) {
i++;
trace("incremented to: "+i);
} else {
i--;
trace("decremented to: "+i);
}
}
if (i==0) {
gotoAndPlay("results");
} else {
warning_mc.loadMovie("images/finishedTooEarly.swf");
}
}

Whenever I trace the i variable it returns NaN. So basically I can't increment it. HELP!


Return to posts index

mark2741
Re: Help with Array Loop
on Oct 12, 2006 at 2:47:37 pm

Well, I finally got the thing to work but I know it's a poor way of coding this. I basically just ditched going with a loop and did multiple if statements to accomplish what I needed to do, and now it works. But clearly this time has taught me that I need to learn how loops really work in real-life ways. I've used them before but for simple things. I thought this was a simple thing and I'm sure it is, but I couldn't get it to work with a loop : (

Here's what I did to get it to work without a loop. If anyone can shed some light on how to do this same thing but with a loop/more elegant code then I'd appreciate it.

//gradeExam
function gradeExam() {
y=0;
if (answers[0] == undefined) {
y++;
}
if (answers[1] == undefined) {
y++;
}
if (answers[2] == undefined) {
y++;
}
if (answers[3] == undefined) {
y++;
}
if (answers[4] == undefined) {
y++;
}
if (answers[5] == undefined) {
y++;
}
if (answers[6] == undefined) {
y++;
}
if (answers[7] == undefined) {
y++;
}
if (answers[8] == undefined) {
y++;
}
if (answers[9] == undefined) {
y++;
}

if (y == 0) {
stopTimer();
gotoAndPlay("results");
} else {
warning_mc.loadMovie("images/finishedTooEarly.swf");
}
}


Return to posts index

lajoiet
Re: Help with Array Loop
on Oct 12, 2006 at 11:46:30 pm

I would use a variable to set to a property if the answers are all completed, and do it something like this:

var allAnswered:Boolean;

function gradeExam() {
allAnswered = true;
for (x = 0; x < answers.length; x++) {
if (allAnswered) {
if (!answers[x]) {
allAnswered = false;
}
}
}
if (allAnswered) {
stopTimer();
gotoAndPlay("results");
}else {
warning_mc.loadMovie("images/finishedTooEarly.swf");
}
}

Basically what this does is when the function is called it sets the allAnswered variable to true, then cycles throught the array, and as long as everything has a value, it stays true, but if one of the index values is empty, it sets it to false (which stops further testing, leaving the value as false) After all that, it uses that value to see if it should grade the array or display the error message.

>Todd
_____________________________________

I'm filling a need. Without me there would be no bad examples.
_____________________________________


Return to posts index


RobotHero
Re: Help with Array Loop
on Oct 12, 2006 at 11:30:22 pm

From your description, I don't know why you would decrement i. Try removing that, and start with the value 0 not null.


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]