Lesson Twenty Four: How it Works - JavaScript

  1. var score = 0;
    Establishes the location and memory space for the score variable that will be increased each time the user achieves a correct answer.

  2. function buttoncorrect() {
    Establishes the buttoncorrect function that is called when the user selects a correct answer (you must associate this function with the correct answer in your HTML file in order to call this function.)

  3. var button = document.getElementById("p1");
    Establishes the location and memory space for the button variable that is identified by the p1 id.

  4. score += 20;
    Adds 20 points each time the buttoncorrect function is called by the HTML.

  5. var points1 = 20;
    button.value = points1;
    Establishes the points1 variable and returns a value of 20 to the button value that is identified by the p1 id.

  6. var button = document.getElementById("tp1");
    button.value = score;
    Establishes the button variable and returns a value of score to the button value that is identified by the tp1 id.

  7. var a1 = document.getElementById("answer1");
    a1.innerHTML = "";
    var a2 = document.getElementById("answer2");
    a2.innerHTML = "";
    var a3 = document.getElementById("answer3");
    a3.innerHTML = "That's Correct:
    Most mollusks live in the water";
    var a4 = document.getElementById("answer4");
    a4.innerHTML = "";
    }
    Establishes the a1, a2, a3, and a4 variables and returns the value (text) that is located at the innerHTML identified by the answer3 id.

  8. function buttonincorrect() {
    Establishes the buttonincorrect function that is called when the user selects an incorrect answer (you must associate this function with the incorrect answers in your HTML file in order to call this function.

  9. var button = document.getElementById("p1");
    var points1 = 0;
    button.value = points1;
    Establishes the button variable and writes a value of 0 to the button that is located by the p1 id.

  10. var a1 = document.getElementById("answer1");
    a1.innerHTML = "Incorrect: The correct answer is: In the water";
    var a2 = document.getElementById("answer2");
    a2.innerHTML = "";
    var a3 = document.getElementById("answer3");
    a3.innerHTML = "";
    var a4 = document.getElementById("answer4");
    a4.innerHTML = "";
    }
    Establishes the a1, a2, a3, and a4 variables and returns the value (text) that is located at the innerHTML identified by the answer1 id. answer2, answer3, and answer4 are cleared by the "" value. The } closes the function.