JavaScript: Need help understanding what's happening in this loop.
-
I want to use a script on my website that scores a multiple choice test, and what I have works pretty well. Before I use this script I want to make sure I understand everything it's doing. I understand most of what is happening, but I don't understand this particular loop:
for (i = 0; i < numberOfQuestions; i++) {
currentElt = i * numberOfChoices;
for (j = 0; j < numberOfChoices; j++) {
currentSelection = form.elements[currentElt + j];
if (currentSelection.checked) {
if (currentSelection.value == correctAnswers[i]) {
numberCorrect++;
break;
}
}
}
}I understand that the first for loop is running through each question, but what is currentElt and why is it equal to the question times the number of answers? What is the second for loop doing with the numberOfChoices variable. The full script is below:
/\*<!\[CDATA\[\*/ var numberOfQuestions = 10; var numberOfChoices = 4; var correctAnswers = new Array(10); correctAnswers = \["a", "b", "b", "a", "d", "a", "b", "b", "a", "c"\]; function scoreQuiz(form) { if (outOfTime == true) { window.alert("You have exceeded the time limit of the test." + "\\n" + "Please try again."); return true; } var numberCorrect = 0; var currentElt; var currentSelection; for (i = 0; i < numberOfQuestions; i++) { currentElt = i \* numberOfChoices; for (j = 0; j < numberOfChoices; j++) { currentSelection = form.elements\[currentElt + j\]; if (currentSelection.checked) { if (currentSelection.value == correctAnswers\[i\]) { numberCorrect++; break; } } } } form.correct.value = numberCorrect; form.percentage.value = ((numberCorrect / numberOfQuestions) \* 100) + "%"; } var remainingTime = 600000; var outOfTime = false; function timeLeft() { if (remainingTime <= 0) { outOfTime = true; } else { setTimeout("timeLeft()", (remainingTime)); remainingTime -= 1; } } /\*\]\]>\*/ </s</x-turndown>
-
I want to use a script on my website that scores a multiple choice test, and what I have works pretty well. Before I use this script I want to make sure I understand everything it's doing. I understand most of what is happening, but I don't understand this particular loop:
for (i = 0; i < numberOfQuestions; i++) {
currentElt = i * numberOfChoices;
for (j = 0; j < numberOfChoices; j++) {
currentSelection = form.elements[currentElt + j];
if (currentSelection.checked) {
if (currentSelection.value == correctAnswers[i]) {
numberCorrect++;
break;
}
}
}
}I understand that the first for loop is running through each question, but what is currentElt and why is it equal to the question times the number of answers? What is the second for loop doing with the numberOfChoices variable. The full script is below:
/\*<!\[CDATA\[\*/ var numberOfQuestions = 10; var numberOfChoices = 4; var correctAnswers = new Array(10); correctAnswers = \["a", "b", "b", "a", "d", "a", "b", "b", "a", "c"\]; function scoreQuiz(form) { if (outOfTime == true) { window.alert("You have exceeded the time limit of the test." + "\\n" + "Please try again."); return true; } var numberCorrect = 0; var currentElt; var currentSelection; for (i = 0; i < numberOfQuestions; i++) { currentElt = i \* numberOfChoices; for (j = 0; j < numberOfChoices; j++) { currentSelection = form.elements\[currentElt + j\]; if (currentSelection.checked) { if (currentSelection.value == correctAnswers\[i\]) { numberCorrect++; break; } } } } form.correct.value = numberCorrect; form.percentage.value = ((numberCorrect / numberOfQuestions) \* 100) + "%"; } var remainingTime = 600000; var outOfTime = false; function timeLeft() { if (remainingTime <= 0) { outOfTime = true; } else { setTimeout("timeLeft()", (remainingTime)); remainingTime -= 1; } } /\*\]\]>\*/ </s</x-turndown>
dude, your code is pretty wonky, try this out to make a quiz http://www.cj-design.com/products/free_downloads/javascript/cjmultiplechoicequiz[^] your time calculation will never work since it is declared at the end of the function. cullentElt is "current Element" you are navigating to all the controls in your form and cheking one by one if they are checked or nor, if they are they shall be matched with the correct answer. i would rather suggest you to use event, for change in option, calculate the score at that time itself, it will make your code hardly 5-10 line. read about "events" in javascript.
-
dude, your code is pretty wonky, try this out to make a quiz http://www.cj-design.com/products/free_downloads/javascript/cjmultiplechoicequiz[^] your time calculation will never work since it is declared at the end of the function. cullentElt is "current Element" you are navigating to all the controls in your form and cheking one by one if they are checked or nor, if they are they shall be matched with the correct answer. i would rather suggest you to use event, for change in option, calculate the score at that time itself, it will make your code hardly 5-10 line. read about "events" in javascript.