WouterPS
WouterPS

Reputation: 1

Javascript if statement comparing text in FOR loop

I have quite a simple problem, but since I'm new to programming I really would like your help.

So basically I have a small HTML/php textboxes on my site where student can fill in answers. I want a for loop to compare these answers to a correct answers and see it there is a match.

What I'm trying to do:

  1. Make a list of all the student answers
  2. Make a list of all the Correct answers
  3. Make a for loop
  4. In that forloop make an if statement that compares the 2 (text)answers
  5. Have a variable names SCORE += 1.

so here is what I have so far:


const Student_Answers_1a = document.getElementsById("Antwoord1a");
const Student_Answers_1b = document.getElementsById("Antwoord1b");

let Answers_Students = [Antwoord1a, Antwoord1b]
let Correct_Answers = ["SELECT title FROM movies;", "SELECT director FROM movies;"]

var Score_Student = 0

for (var i = 0; i < Answers_Students.length; i++) {
  if (Answers_Students[i] === Correct_Answers[i]) {
    Score_Student += 1;
  } else {
  
  }
}

Upvotes: 0

Views: 108

Answers (1)

Ma Kobi
Ma Kobi

Reputation: 896

document.getElementById() returns the DOM element. To get the value of that element you need .value property:

const Student_Answers_1a = document.getElementsById("Antwoord1a").value;

Upvotes: 1

Related Questions