I’m building this browser-based quiz for a class project and would like to know what would be the best way to delay the answer prompt for a few seconds so that my UI can update?
function generateQuestion() {
var n = Math.floor(Math.random() * questions.length);
questions[n].displayQuestion();
var answer = (prompt('Please select the correct answer.'));
if(answer !== 'exit'){
questions[n].checkAnswer(parseInt(answer));
generateQuestion();
}
}
generateQuestion();
Hi @CooKachoo, the real solution would be not using a UI-blocking modal dialog in the first place… not sure if this would be over the top for your class, but you can also get user input from an actual input element like so:
This works so much better! However I’m having an issue displaying all the possible answers… It’s not returning the options quite like I was in the console…
In each iteration of the for loop, you’re overwriting the answer HTML so eventually you’re only displaying the last one in the list. Instead, you might append it using the += operator:
Question.prototype.displayQuestion = function () {
var ui = {
question: document.getElementById('ui-question'),
answers: document.getElementById('ui-answers')
}
ui.question.innerHTML = this.question
ui.answers.innerHTML = ''
for (var i = 0; i < this.answers.length; i++) {
ui.answers.innerHTML += i + ': ' + this.answers[i] + '<br>'
}
}
BTW I’d suggest using an actual ordered list element, as this is what you’re displaying.
I noticed a few things that haven’t been covered in my class yet…
question: (HTML Element) is this like creating a variable inside of a variable? Would this be an object?
ui.answers.innerHTML = ’ ’ ui.answers.innerHTML += i + ': ’ + this.answers[i] + ‘ ’ Secondly what makes the first ui.answers with the blank string necessary and not redundant?
Thanks for taking the time to walk me through this