I'm trying to compare the input from a prompt window to the position of the correct answer in an array within an array.
This function logs the question and possible answers to the console. It then logs the correct answer, but does not recognize the input as correct ie. it will always show the else statement. Code:
questionArray[randomQ].questionPrompt();
var currentQ = randomQ;
Question.prototype.answerPrompt = function(){
var tryQ = prompt("Enter number of the correct answer.");
if (currentQ === tryQ){
console.log('The correct answer is ' + this.answer)
} else {
console.log('Try again. ' + this.answer)
}
};
Console log. The final line comes after input of 0:
Question?
0) answer 0 - correct answer
1) answer 1
2) answer 2
Try again. 0
If I use
if (questionArray.answerArray[currentQ] === tryQ)
then the correct array item is found, and listed as undefined in a TypeError. How do I use that array item to compare to the prompt answer?