So basically I have two arrays: A word array and a definition array. The word array consists of a set of words that are matched by the a respective definition in the definition array: i.e word[0] = definition[0] and so on. What I wish to achieve is I want to give the user a quiz, where a random word from the word array comes out and user has to input the definition in a text box and such that no word is repeated and no word is missed out provided by a score at the end when user enters the last definition. I am able to achieve some of it, here is my ugly code:
var word = "<?php echo $word; ?>";//getting words from db to a js array
var def = "<?php echo $def; ?>";//same for definition
var finalword = word.split(",");//final word array
var finaldef = def.split(",");//final definition array
function randomize() {
document.getElementById("answer").value = "";
document.getElementById("success").innerHTML = "";
document.getElementById("fail").innerHTML = "";
var random = finalword[Math.floor(Math.random()*finalword.length)];//randomize the word from array
document.getElementById("question").innerHTML = random;
for(var i=0;i<=finalword.length;i++) { //find the index of the random word and match it with the index of definition
if(finalword[i]==random) {
console.log(i);
var randomdef = i;
answerdef = finaldef[randomdef];
console.log(answerdef);
}
}
}
function extract(a) {
//check if indices are equal
var answer = document.getElementById("answer").value;
console.log(answer);
if(answerdef == answer) {
var right = document.getElementById("success");
document.getElementById("fail").innerHTML = "";
right.innerHTML = "Whoopie, correct answer, let's move onto the next question.";
right.className = right.className + "animated infinite pulse";
}
else {
var wrong = document.getElementById("fail");
var input = document.getElementById("input");
input.className = input.className + "animated infinite shake";
wrong.innerHTML = "Oopsie, hold your horses. The answer is incorrect.";
wrong.className = wrong.className + "animated infinite pulse";
}
} //ignore the css and other calls.