Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从数组值计算分数_Javascript - Fatal编程技术网

Javascript 从数组值计算分数

Javascript 从数组值计算分数,javascript,Javascript,正如标题所示。我该怎么做 我一定会使用纯JS,而且我对它很陌生(就像真的很新一样)。所以请原谅我的noob问题。:) 我的数组如下所示: var questionArr = [ { 'questionId' : 'Question1', 'question' : 'Q1: How many Objects are there? ', 'choices' : [ {"id" : "10", "tf" : false, "points" : 0},

正如标题所示。我该怎么做

我一定会使用纯JS,而且我对它很陌生(就像真的很新一样)。所以请原谅我的noob问题。:)

我的数组如下所示:

var questionArr = [
{
    'questionId' : 'Question1',
    'question' : 'Q1: How many Objects are there? ',
    'choices' : [
        {"id" : "10", "tf" : false, "points" : 0},
        {'id' : "11", "tf" : false, "points" : 0},
        {'id' : "12", "tf" : false, "points" : 0},
        {'id' : "15", "tf" : true, "points" : 10},
        {'id' : "16", "tf" : false, "points" : 0}
    ],
    'Mchoice' : false,
    'completed' : false
},
{
    'questionId' : 'Question2',
    'question' : 'Q2: some Question will go here? ',
    'choices' : [
            {'id' : "answer1", "tf" : true, "points" : 5},
            {'id' : "answer2", "tf" : false, "points" : 1},
            {'id' : "answer3", "tf" : false, "points" : 1},
            {'id' : "answer4", "tf" : true, "points" : 10}
        ],
        'Mchoice' : true,
        'completed' : false
    },
而且还在进行中

每个选项都显示为单选按钮或复选框。取决于“McChoice”是否为假/真。当我创建它们时,它们中的每一个都会得到“tf”的值,我可以检查所选答案是否正确

我的目标是在后台计算分数,如果答案正确,则将分数相加,如果答案错误,则减去分数,但减去分数时我不能低于0。在回答完所有问题并按下提交按钮后,我想看看每个问题的总分中有多少是达到的。比如问题1的10分中有X分,而在这一页的末尾,总分是:100分中有X分。让我们只说100是可以达到的总分数


我希望你知道我的意思,并能帮助我,因为我真的坚持了一天或更长的时间。不知道如何做到这一点。再说一次,我是JS的新手,所以如果这是一个非常愚蠢的问题,请不要对我太苛刻:)

首先,这不是一个愚蠢的问题。您需要有选定的选项,您可以逐个检查它们,或者先将它们存储在数组中,然后计算分数(就像我所做的那样):

在这种情况下,总数为15。希望您能理解这些循环,它们非常简单


一旦你实现了这个数组,就使用嵌套循环。这很简单,但是你如何知道用户选择了哪个选项?如果McChoice表示用户的答案正确与否,我在那里看到了多个正确答案,那么如何确切地知道用户选择了哪个答案(var i=0;ivar questionArr = [ { 'questionId' : 'Question1', 'question' : 'Q1: How many Objects are there? ', 'choices' : [ {"id" : "10", "tf" : false, "points" : 0}, {'id' : "11", "tf" : false, "points" : 0}, {'id' : "12", "tf" : false, "points" : 0}, {'id' : "15", "tf" : true, "points" : 10}, {'id' : "16", "tf" : false, "points" : 0} ], 'Mchoice' : false, 'completed' : false }, { 'questionId' : 'Question2', 'question' : 'Q2: some Question will go here? ', 'choices' : [ {'id' : "answer1", "tf" : true, "points" : 5}, {'id' : "answer2", "tf" : false, "points" : 1}, {'id' : "answer3", "tf" : false, "points" : 1}, {'id' : "answer4", "tf" : true, "points" : 10} ], 'Mchoice' : true, 'completed' : false } ] var selectedChoicesArray = ["10", "answer4"] //choices that user selected as answers var sum = 0; for(var i=0; i<questionArr.length; i++) { for(var j=0; j<questionArr[i].choices.length; j++){ if(questionArr[i].choices[j].id == selectedChoicesArray[i]) //the choice which user selected sum += questionArr[i].choices[j].points //matched choice's points will be added to the sum } } alert("Sum: "+sum);
var selectedChoicesArray = ["15", "answer1"];