这个Javascript计数循环如何工作? - 创建测验的输出

时间:2017-01-23 21:35:10

标签: javascript arrays json

我正在尝试构建一个不是“正确”或“错误”的javascript测验,但会按比例提供输出。这些测验是为了个人品味,个性等。所以,“你是什么超级英雄?”

我一直在尝试使用此JSON / Javascrpt:https://github.com/jbierfeldt/bf-quiz/blob/master/bf-quiz.js

我真的很喜欢calcResult。我一直在尝试调试它,我无法弄清楚它在做什么。它是否跟踪单个组highest的最高数量,然后仅在出现更大的内容时修改highest_score我无法弄清楚这个输出如何与定义的数字结果对齐。

calcResult = function calcResult() {
    var highest = 0;
    for (var i = 0; i < results.length; i++) {
        results[i].countof = 0;
        for (var j = 0; j < userAnswers.length; j++) {
            if (userAnswers[j] == results[i].result.id) {
                results[i].countof++;
            }
        }
        if (results[i].countof > highest) {
            highest = results[i].countof;
            highest_score = results[i];
        }
    }
    //disable the inputs after the quiz is finished
    writeResult();
    disableAnswers();
},

2 个答案:

答案 0 :(得分:1)

这很简单。我为你的代码添加了(略微通用的)注释。

它的作用是找到大多数用户选择的结果作为答案(无论你的用例是什么结果)。

calcResult = function calcResult() {
    // used to keep track of the maximum userAnswers for one result
    var highest = 0;

    // iterate through all results...
    for (var i = 0; i < results.length; i++) {

        // used to count how many userAnswers exist for this result
        results[i].countof = 0;
        // iterate through userAnswers and...
        for (var j = 0; j < userAnswers.length; j++) {
            // find userAnswers that are for current result
            if (userAnswers[j] == results[i].result.id) {
                // if we are here, we found a userAnswer for the
                // current result. Therefore, we increment the
                // number of userAnswers for the result.
                results[i].countof++;
            }
        }

        // check if the current result has the highest number of    
        // userAnswers so far
        if (results[i].countof > highest) {
            // if so, set the new maximum number of userAnswers per
            // result to the number of userAnswers of the current
            // result
            highest = results[i].countof;

            // store this result as the one with the most userAnswers
            highest_score = results[i];
        }
    }
    //disable the inputs after the quiz is finished
    writeResult();
    disableAnswers();
},

答案 1 :(得分:0)

highest是分数的数值。 highest_score是得分最高的引用对象。它在某些方面确实是多余的代码。最后一个if语句可以改进为:

    if (!highest_score || results[i].countof > highest_score.countof) {
        highest_score = results[i];
    }

然后您可以完全删除highest变量,然后引用highest_scorehighest_score.countof。比如这个:

calcResult = function calcResult() {
    for (var i = 0; i < results.length; i++) {
        results[i].countof = 0;
        for (var j = 0; j < userAnswers.length; j++) {
            if (userAnswers[j] == results[i].result.id) {
                results[i].countof++;
            }
        }
        if (!highest_score || results[i].countof > highest_score.countof) {
            highest_score = results[i];
        }
    }
    //disable the inputs after the quiz is finished
    writeResult();
    disableAnswers();
},

但我不建议这样做。该库可能会在代码中的其他位置引用highest