Java Reference
In-Depth Information
The ask() function also needs updating, so that it provides a number of options for the
player to select instead of just an input field for the player to fill in. This makes the ask()
function quite a bit longer than it was before. It needs to be changed as follows:
function ask(question) {
console.log("ask() invoked");
// set the question.asked property to true so it's not
asked again
question.asked = true;
update($form,quiz.question + question.question + "?");
// create an array to put the different options in and a
button
variable
var options = [], button;
var option1 = chooseOption();
options.push(option1.answer);
var option2 = chooseOption();
options.push(option2.answer);
// add the actual answer at a random place in the
options array
options.splice(random(0,2),0,question.answer);
// loop through each option and display it as a button
options.forEach(function(name) {
button = document.createElement("button");
button.value = name;
button.textContent = name;
$form.appendChild(button);
});
// choose an option from all the possible answers but
without
choosing the same option twice
function chooseOption() {
var option =
quiz.questions[random(quiz.questions.length) - 1];
// check to see if the option chosen is the current
question or
already one of the options, if it is then recursively
 
Search WWH ::




Custom Search