Click here to Skip to main content
15,897,122 members
Home / Discussions / Web Development
   

Web Development

 
QuestionJavaScript: Need help understanding what's happening in this loop. Pin
durango7724-Apr-09 13:38
durango7724-Apr-09 13:38 
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:
<script type="text/javascript">
        /*<![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;
            }
        }
        /*]]>*/
    </script>

If anyone can help me understand this, or show me a better (simpler?) method of achieving the same thing I would greatly appreciate it.
AnswerRe: JavaScript: Need help understanding what's happening in this loop. Pin
Parinay Bansal24-Apr-09 18:41
Parinay Bansal24-Apr-09 18:41 
GeneralRe: JavaScript: Need help understanding what's happening in this loop. Pin
durango7725-Apr-09 3:40
durango7725-Apr-09 3:40 
Questionjavscipt: hex to string conversion problem: Pin
Parinay Bansal24-Apr-09 10:35
Parinay Bansal24-Apr-09 10:35 
Questionhelp me Pin
tidelgl24-Apr-09 7:08
tidelgl24-Apr-09 7:08 
AnswerRe: help me Pin
Rolando CC24-Apr-09 7:50
professionalRolando CC24-Apr-09 7:50 
AnswerRe: help me Pin
Yusuf24-Apr-09 10:18
Yusuf24-Apr-09 10:18 
QuestionAuthorization Required Pin
p_196023-Apr-09 20:56
p_196023-Apr-09 20:56 
AnswerRe: Authorization Required Pin
Paddy Boyd24-Apr-09 1:54
Paddy Boyd24-Apr-09 1:54 
AnswerRe: Authorization Required Pin
Yusuf24-Apr-09 6:10
Yusuf24-Apr-09 6:10 
Questionretrieve image Pin
LiYS23-Apr-09 17:20
LiYS23-Apr-09 17:20 
QuestionAn article submission system Pin
jesarg23-Apr-09 17:14
jesarg23-Apr-09 17:14 
AnswerRe: An article submission system Pin
Yusuf24-Apr-09 6:11
Yusuf24-Apr-09 6:11 
QuestionAjax hover menu Pin
Mohammad Al Hoss23-Apr-09 14:37
Mohammad Al Hoss23-Apr-09 14:37 
AnswerRe: Ajax hover menu Pin
Yusuf23-Apr-09 15:28
Yusuf23-Apr-09 15:28 
QuestionCSS and linking to your content [modified] Pin
rbwest8623-Apr-09 11:32
rbwest8623-Apr-09 11:32 
AnswerRe: CSS and linking to your content Pin
DoctorMick24-Apr-09 4:45
DoctorMick24-Apr-09 4:45 
QuestionCSS Issue with VS2008 Pin
DotNetMan23-Apr-09 10:24
DotNetMan23-Apr-09 10:24 
AnswerRe: CSS Issue with VS2008 Pin
rbwest8623-Apr-09 12:04
rbwest8623-Apr-09 12:04 
QuestionDisplaying multiple pages in the same page Pin
armasramirez23-Apr-09 5:47
armasramirez23-Apr-09 5:47 
QuestionclickOnce problem Pin
Shital@icon22-Apr-09 22:02
Shital@icon22-Apr-09 22:02 
AnswerRe: clickOnce problem Pin
saanj28-Apr-09 21:37
saanj28-Apr-09 21:37 
QuestionWeb Development – Six Questions inside! Pin
rbwest8622-Apr-09 16:01
rbwest8622-Apr-09 16:01 
AnswerRe: Web Development – Six Questions inside! Pin
Marc Firth22-Apr-09 22:04
Marc Firth22-Apr-09 22:04 
QuestionIs this do-able? Sync web data with thumb drive device Pin
Hypermommy21-Apr-09 9:20
Hypermommy21-Apr-09 9:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.