0

我的程序正在响应,表现出意外的行为。

selectQuestion() 的预期行为:

随机选择一个问题类别并检查所需的难度级别。搜索问题列表,将任何符合条件但最近未播放的问题添加到潜在问题列表中。如果没有符合此随机选择标准的问题,则重复上述步骤,直到找到问题。

selectQuestion() 的实际行为:

selectQuestion()将选择 中的问题recentQuestions[],而不是随机选择新类别。

附加信息:

dda[]是一个对象数组,每个对象都有 acategory和 a difficulty

question[]是一个对象数组,每个对象都有一个question, answers[], correctAnswer,categorydifficulty.

recentQuestions[]是一个整数数组。(每个整数是一个问题的索引question[]

function selectQuestion()
{
    //Create an array we can use to store the numbers of any questions which meet our criteria
    var potentialQuestions = new Array();
    // While there are no questions which meet our criteria, pick new critieria
    // (This prevents the program from getting 'stuck' if criteria can't be met)
    while(potentialQuestions.length == 0) {
        // Select a category at random, retrieve the difficulty we're looking for
        var randomSelection = Math.floor(Math.random()*dda.length);
        var category = dda[randomSelection].category; 
        var difficulty = Math.floor(dda[randomSelection].difficulty+0.5);
        // For each question we have (in question[]) 
        for(q = 0; q < question.length; q++) {
                    // If the category and the difficulty meet our criteria
            if(question[q].category == category & question[q].difficulty == difficulty) {
                            // Check if the question has been played recently
                            // by looping through recentQuestions[] 
                var playedRecently = false; 
                for(r = recentQuestions.length; r=0; r++) {
                    if(recentQuestions[r] == q) {
                        playedRecently = true; 
                    }
                }
                // If the question has not been played recently 
                if(!playedRecently) {
                                    // Add it to potentialQuestions[]
                    potentialQuestions.push(q); 
                }
            }
        }
    }

    // Select a question at random from our potential questions 
    var selectedQuestion = potentialQuestions[Math.floor(Math.random()*potentialQuestions.length)]; 

    // If 5 recent questions have been stored, remove the oldest 
    if (recentQuestions.length == 5) {
        recentQuestions.shift(); 
    }

    // Add the selected level to recentQuestions[]
    recentQuestions.push(selectedQuestion); 
    return selectedQuestion; 
}

我不确定这种行为来自哪里。任何想法都将受到欢迎!谢谢!

解决了!

for (r= recentQuestions.length; r=0; r++)事实上应该是for (r=0; r<recentQuestions; r++)- 绝对是那些日子之一!呸!谢谢大家:)

4

2 回答 2

2

第二个答案和第一个一样。您在 for 循环条件中有一个分配,而不是比较。(=而不是==)。

看起来你也打算倒计时?那么,你的增量应该是r--

于 2013-04-05T16:17:48.647 回答
0

在您的 if 语句中,您有一个&; 你可能想要一个&&. &是按位与,而不是逻辑与。

于 2013-04-05T16:10:44.593 回答