2014-08-27 74 views
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 
    }, 

和持续。

对于每个选项都显示为单选按钮或复选框。取决于“Mchoice”beeing false/true。当我创建它们时,它们每个都获得“tf”的值,我可以检查所选答案是否正确。

我的目标是获得在后台计算的分数,如果答案是正确的话就加上分数,如果答案是错误的,就减去分数,但是当减去时不能低于0。在回答所有问题之后,我按下提交按钮,我想查看每个问题的总分数中有多少个已经达到。就像问题1中10分的X和总分10分中的X一样。让我们假设100是可以实现的总分数。

我希望你知道我的意思,并可以帮助我,因为我真的坚持这一天或更多的现在。不知道如何实现这一点。再次,即时通讯新的JS,所以请不要对我苛刻如果这是一个真正愚蠢的问题到底:)

+0

使用,一旦你达到这个数组嵌套循环这一点。 – 2014-08-27 08:03:29

+0

这很简单,但你怎么知道用户选择了哪个选择? – 2014-08-27 08:05:08

+0

如果MChoice表示用户的答案是否正确,那么我在那里看到多个正确的答案,所以如何确切地知道用户选择 – 2014-08-27 08:06:12

回答

0

首先它不是一个愚蠢的问题。你需要有选择的选择,你可以通过一个检查他们的一个或他们先存储在数组中,然后计算出得分(像我一样):

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 
    } 
] 
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); 

在这种情况下,selectedChoicesArray礼物,用户选择的第一选择对于问题1和答案2的第四选择,因此,总和将是10!如果将其更改为:

var selectedChoicesArray = ["15", "answer1"]; 

在这种情况下总和将为15。希望你能理解这些循环,他们相当直接。

See the demo here

+0

,如果你想检查并添加作为用户选择的总和(一次一个),那么它会这样完成:http://jsfiddle.net/85bkfaqh/2/ – 2014-08-27 08:28:08

+0

哇,这太棒了。非常感谢。它并不像我想的那么复杂,我真的只是一个初学者,并认为这是一种更大的代码和更复杂的方式。你真的确实救了我; – 2014-08-27 08:37:11

+0

随时欢迎你..快乐编码(: – 2014-08-27 08:38:16