嗨,所以我有一個非常基本的JavaScript HTML測驗工作,但是我想最終是一個評估類型的測驗,可以說基於3類15個問題(如酒精中毒,抑鬱症,藥物濫用)例如HTML Javascript自我評估問卷/問卷
你多久喝一次酒? - 所有的時間 - 偶爾 - 從未
玩家回答所有15個問題,並在年底這取決於他們是如何回答的問題,他們會被分配例如一個類別你的類別是藥物濫用等
我在想,也許每個類別都有自己的計數器,並將值應用於每個可能的答案,例如,所有時間都給出3分,偶爾得2分等。當玩家回答問題時,將值存儲在相應的類別中,並且最後將每個類別的分數相加,得分最高的類別得到分配給玩家?
任何幫助,將不勝感激:)
HTML:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2 id="test_status"></h2>
<div id="test"></div>
</body>
</html>
CSS:
<style>
div#test {
border:#000 1px solid;
padding: 10px 40px 40px 40px;
}
</style>
JS:
<script>
var pos = 0, test, test_status, question, choice, choices, chA, chB, chC, correct = 0;
var questions = [
["How often do you drink?", "All the time", "Often", "Never", "B"],
["Do you ever feel sad for no reason?", "All the time", "Often", "Never", "C"],
["Have you ever tried drugs", "All the time", "Often", "Never", "C"],
["Do you feel uneasy around people", "All the time", "Often", "Never", "C"]
];
function _(x) {
return document.getElementById(x);
}
function renderQuestion() {
test = _("test");
if(pos >= questions.length) {
test.innerHTML = "<h2>Your Category is </h2>";
_("test_status").innerHTML = "Test Completed";
pos = 0;
correct = 0;
return false;
}
_("test_status").innerHTML = "Question "+(pos+1)+" of"+questions.length;
question = questions[pos] [0];
chA = questions[pos] [1];
chB = questions[pos] [2];
chC = questions[pos] [3];
test.innerHTML = "<h3>"+question+"</h3>";
test.innerHTML += "<input type='radio' name='choices' value='A'> "+chA+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='B'> "+chB+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='C'> "+chC+"<br><br>";
test.innerHTML +="<button onclick='checkAnswer()'>submit Answer</button>";
}
function checkAnswer() {
choices = document.getElementsByName("choices");
for (var i=0; i<choices.length; i++) {
if(choices[i].checked) {
choice = choices[i].value;
}
}
if(choice == questions[pos] [4]) {
correct++;
}
pos++;
renderQuestion();
}
window.addEventListener("load", renderQuestion, false);
</script>
你有一個編程的問題,或者這是對邏輯,你的問卷? – 2015-04-02 15:46:23
我試圖找出我將如何去編碼我上面提到的..或者如果任何人有更好的解決方案 – TryingAtCode 2015-04-02 15:48:35