2014-08-27 40 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