mercredi 26 octobre 2016

If clauses in for loop acts weird

I have a function, which collects inputted answer and checked if collected answer is the same as it is the solution. Questions are asked randomly. Questions and solutions are in separate .txt file.

I have a file with some words and the program always fulfil its condition if the last word from the list is correctly answered, even though if other answers from other questions are correct.

Any suggestions?

var besedeNemske = []; //here are previuously inputed words from  file aka solutions (five words)
var besedePrevodi = []; // here are previuously inputed words from file aka questions (five words)
var random = 0;

function nastaviRandom(){
    random = Math.floor(Math.random()*5);
}

function noviGlagol(){

    nastaviRandom();

    document.getElementById("vprasanje").innerHTML = besedePrevod[random];

}

function preveriOdgovor() {

  var odgovorjeno = document.getElementById("myAnswer").value;

  var index = 0;

  for (var i = 0; i < besedeNemske.length; i++) {
    if (odgovorjeno == besedeNemske[i]) {
      index = i;
      break;
    }
  }
  if (index == random) {
    document.getElementById("odgovor").innerHTML = "Pravilni odgovor";
  } else {
    document.getElementById("odgovor").innerHTML = "Napačen odgovor.   Pravilni odgovor je " + (besedeNemske[random]) + "";
  }
}

Aucun commentaire:

Enregistrer un commentaire