こんにちは、簡単なオンライン クイズでユーザーの結果を指定された電子メールに送信するための最善の方法を見つけるのに苦労しています。私はJavascriptにかなり慣れていないので、誰かが私を正しい方向に向けることができれば、本当に感謝しています.
var allQuestions = [{question: "2 + 2", choices: ["4", "8", "10", "2"], correctAnswer:0},
{question: "6 + 3", choices: ["7", "3", "5", "9"], correctAnswer:3},
{question: "4 + 4", choices: ["8", "7", "2", "9"],
correctAnswer:0},
{question: "5 + 0", choices: ["4", "5", "3", "9"], correctAnswer:1},];
//you can access checkbox name through an array
//match array number to number in allQuestions array
var questionNum = 0;
var scoreNum = 0;
var makeQuestions = "";
$(document).ready(function() {
makeQuestions = function() {
if(questionNum === allQuestions.length){
$("input[value=SUBMIT]").remove();
$("#questions").text(" Done!Please click the button below to submit your results. Your score is" + " " + scoreNum);
}
else{
$("#questions").text(allQuestions[questionNum].question);
for(var i=0; i<allQuestions[questionNum]['choices'].length;i++){
$('#words').append('<input type="radio" name="buttons">' + allQuestions[questionNum]['choices'][i] + '</input');
}
}
}
makeQuestions();
});
var checkQuestions = function() {
var lenG = document.getElementsByName("buttons").length;
console.log(lenG);
var rightAnswer = allQuestions[questionNum]['correctAnswer'];
for (var i=0; i<lenG; i++){
if (document.getElementsByName("buttons")[i].checked === true) {
console.log(i);
console.log(document.getElementsByName("buttons")[i].checked);
//compare value to what was inputted
if(i === rightAnswer){
scoreNum +=1;
alert("Correct! Your score is" +" " + scoreNum);
}
else{
alert("False! Your score is still"+ " " + scoreNum );
}
}
}
questionNum = questionNum +1;
$("#words").empty();
makeQuestions();
}