数组对象的数组

时间:2017-06-15 16:32:50

标签: javascript

我有一个名为'问题'我有一个与之关联的对象变量,它也是一个名为' choices'。

的数组
function displayScore() 
{
var score = $('<p>',{id: 'question'});
var points = 0;
for (var i = 0; i < selections.length; i++) 
{
    for (var j = 0; j < questions[i].choices.length; j++)
    {
        if (selections[i] === questions[i].choices[j]) 
        {
            points = points + questions[i].choiceValues[j];
        }
    }
}
score.append('You got ' + points + ' points!');
return score;
}

我想使用&#39;选择的长度&#39;在for循环中,如下所示。这种语法是否正确?我似乎无法看到“积分”。变量增加。

app.post('/:board_name(\\[a-z0-9]+)/:type(\\d{2}):tab(\\d{2}):id(\\d+)', board.postEntry)

选择的一个例子&#39;将是:[0,4,0,1,0,1,0,1]。

1 个答案:

答案 0 :(得分:0)

问题是selections[i]number (0)questions[i].choices[j]string ('Business to Business - Provide products and/or services to other businesses'),因此,if (selections[i] === questions[i].choices[j])不会工作(总是false)。

您可以检查选择的索引:

if (selections[i] === j) 
{
    points = points + questions[i].choiceValues[j];
}

此外,内部for没用,你可以这样做:

function displayScore() 
{
    var score = $('<p>',{id: 'question'});
    var points = 0;
    for (var i = 0; i < selections.length; i++) {
        points += questions[i].choiceValues[selections[i]];
    }
    score.append('You got ' + points + ' points!');
    return score;
}