html5 - Javascript code exhibiting unexpected logical behaviour -
my program responding exhibiting unexpected behaviour.
expected behaviour of selectquestion():
select category of question @ random , check difficulty level required. search through list of questions, adding meeting criteria have not been played list of potentialquestions. if there no questions meeting randomly chosen criteria, repeat above step until questions found.
actual behaviour of selectquestion():
selectquestion()
select questions in recentquestions[]
, rather selecting new category @ random.
additional information:
dda[]
array of objects, each category
, difficulty
.
question[]
array of objects, each question
, answers[]
, correctanswer
, category
, difficulty
.
recentquestions[]
array of integers. (each integer index of question in question[]
)
function selectquestion() { //create array can use store numbers of questions meet our criteria var potentialquestions = new array(); // while there no questions meet our criteria, pick new critieria // (this prevents program getting 'stuck' if criteria can't met) while(potentialquestions.length == 0) { // select category @ random, retrieve difficulty we're looking var randomselection = math.floor(math.random()*dda.length); var category = dda[randomselection].category; var difficulty = math.floor(dda[randomselection].difficulty+0.5); // each question have (in question[]) for(q = 0; q < question.length; q++) { // if category , difficulty meet our criteria if(question[q].category == category & question[q].difficulty == difficulty) { // check if question has been played // looping through recentquestions[] var playedrecently = false; for(r = recentquestions.length; r=0; r++) { if(recentquestions[r] == q) { playedrecently = true; } } // if question has not been played if(!playedrecently) { // add potentialquestions[] potentialquestions.push(q); } } } } // select question @ random our potential questions var selectedquestion = potentialquestions[math.floor(math.random()*potentialquestions.length)]; // if 5 recent questions have been stored, remove oldest if (recentquestions.length == 5) { recentquestions.shift(); } // add selected level recentquestions[] recentquestions.push(selectedquestion); return selectedquestion; }
i'm not sure behaviour coming from. thoughts welcome! thanks!
solved!
for (r= recentquestions.length; r=0; r++)
should have in fact been for (r=0; r<recentquestions; r++)
- 1 of days! gah! :)
the second answer unto first. have assignment in for-loop condition rather comparison. (=
instead of ==
).
it looks intend counting down? so, should increment r--
?
Comments
Post a Comment