如果排名總是相同,名稱最上面的答案會得到+3,第二個+2,第三個+1和最後一個+0到它的關聯actegory,你可以通過使用一些數據結構簡化這個整個計算。 基本上,您只需要將每個可能的答案與一個類別相關聯,然後將所有分數統計在一起。
最大的好處是,你可以再補充問題和類別不改變代碼的其餘部分。否則,如果添加一個類別,則必須爲每個添加的問題重新編寫整個if/else邏輯,並編輯所有已有的if/else。
就在這裏展示的原則。應該更好地檢查用戶輸入來擴展代碼。並obv使用你自己的問題。等等
// Array with all used categories
var categories = [
'orientaal',
'houtig',
'bloemig',
'aromatisch',
'chypre',
'citrus'
],
// Array containing all the questions and the category of each answer
questions = [
{
'text' : 'Welk element spreekt jou het meest aan? Zet de letters van hoog naar laag (Bijv. DBAC).',
'answers' : {
'A' : {
'val' : 'Vuur',
'category' : 'orientaal'
},
'B' : {
'val' : 'Lucht',
'category' : 'bloemig'
},
'C' : {
'val' : 'Aarde',
'category' : 'houtig'
},
'D' : {
'val' : 'Water',
'category' : 'citrus'
}
}
}, {
'text' : 'Welk dier spreekt jou het meeste aan?',
'answers' : {
'A' : {
'val' : 'Draak',
'category' : 'orientaal'
},
'B' : {
'val' : 'Arend',
'category' : 'aromatisch'
},
'C' : {
'val' : 'Wolf',
'category' : 'houtig'
},
'D' : {
'val' : 'Slang',
'category' : 'chypre'
}
}
}, {
'text' : 'Welke locatie spreekt je het meeste aan?',
'answers' : {
'A' : {
'val' : 'Dennenbos',
'category' : 'houtig'
},
'B' : {
'val' : 'Steppe',
'category' : 'orientaal'
},
'C' : {
'val' : 'Oerwoud',
'category' : 'citrus'
},
'D' : {
'val' : 'Heide',
'category' : 'bloemig'
}
}
}
],
// Array to save the answers of the user.
userAnswers = [],
// Var to create the results.
scoreList;
// Let's 'fake' an interface that asks the questions. Replace this with your own form and functions that push the answers to the userAnswers array.
questions.forEach(function(questionObj, index) {
// Create the text for the possible answers from our questionObject.
var answerText = Object.keys(questionObj.answers).map(function(letter) {
// We just return the letter, plus a dot, plus the value corresponding with that letter.
return letter + '.: ' + questionObj.answers[ letter ].val;
}).join(', '),
// Ask the question, using the text inside our questionObj.
prompt = window.prompt('Vraag ' + (index + 1) + ': ' + questionObj.text + ' ' + answerText, '');
// Check if a 'valid' answer was entered. This should be expanded to also check that the input consists of some combination of ABCD
// if (prompt && prompt.length === 4) userAnswers.push(prompt);
// else alert('Gelieve alle 4 de letters in volgorde te zetten');
// But for now we'll just accept any answer for testing purposes.
userAnswers.push(prompt);
});
// Now let's check the score of the answers. We can reduce the users' answers into one value object that we can create by reducing our categories into a 'score list': A plain object with the category as the key adn the count as the value.
scoreList = userAnswers.reduce(function(scoreList, answer, index) {
// Since our answers are in the same order as our questions, we can just use the idnex to get the question associated with this answer.
var questionObj = questions[ index ],
// By splitting the answer, we get an array containing the 4 individual letters.
answerLetters = answer.split('');
// For each letter that is part of the answer, add the correct value to our score list.
answerLetters.forEach(function(letter, index) {
// The answered letter gives us the correct category
var category = questionObj.answers[ letter ].category;
// We can just use a static array to transform the index into the score: index 0 = score 3, i1 = s2, i2 = s1, i3 = s0
scoreList[ category ] += [ 3, 2, 1, 0 ][ index ];
});
return scoreList;
}, categories.reduce(function(scoreList, category) {
scoreList[ category ] = 0;
return scoreList;
}, {}));
// Report the results
alert(JSON.stringify(scoreList));
'if(q1.charAt(0)=='A'||'a')'不會做你認爲的事情。在if語句if(q1.charAt(1)=='A'|| q1.charAt(1)=='a')中更改像這樣( –
) –