Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript: Need help understanding what's happening in this loop.

JavaScript: Need help understanding what's happening in this loop.

Scheduled Pinned Locked Moved Web Development
questionjavascriptdata-structurestoolshelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    durango77
    wrote on last edited by
    #1

    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>
    
    P 1 Reply Last reply
    0
    • D durango77

      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>
      
      P Offline
      P Offline
      Parinay Bansal
      wrote on last edited by
      #2

      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.

      D 1 Reply Last reply
      0
      • P Parinay Bansal

        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.

        D Offline
        D Offline
        durango77
        wrote on last edited by
        #3

        Thanks for the help. I know that timer isn't working; I will probably fix it today. Thanks again.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups