-1
  var q1textbox = document.getElementsByClassName('q1');
  var q2radio = document.getElementsByClassName('q2');

  var correct = 0;
  var wrong = 0;
 var total = 10;
var grade = 0;

function doTheCorrecting() {
// Question one
if (q1textbox.value == 'Scott') {
        correct+= 1;
        console.log('q1 was correct ' + 'correct=' + correct);
} else {
    wrong+= 1;
    console.log('q1 is wrong ' + 'wrong=' + wrong);
}

// Question two
if (q2radio.checked = true) {
    correct+= 1;
    console.log('q2 was correct ' + 'correct=' + correct);
} 
if (q2radio.checked = false) {
    wrong += 1;
    console.log('q2 is wrong ' + 'wrong=' + wrong);
}
// Question three 

// Question four

// Question five

grade = total - wrong;
console.log('grade is ' + grade)
}

I've been trying to figure out how to grade a quiz I made and I've spent around a hour trying to figure out how to do it. The main error is that even when I type Scott into the textbox, it still adds 1 to wrong. How can I fix this?

Tler
  • 154
  • 1
  • 11

1 Answers1

2

You are using getElementsByClassName() method which returns a HTMLCollection (or an array-like object of all child elements which have all of the given class names)
So to access values from a particular element among them, you've to simply do:

var q1textbox = document.getElementsByClassName('q1');
console.log(q1textbox[0].value); //if it's the first text box you are interested in
Rahul Bharadwaj
  • 2,555
  • 2
  • 18
  • 29