Example Description
In this example, we create a quiz that randomly chooses 5 questions from a collection. Once the site visitor answers all the quiz questions and submits them, we validate their answers, show the correct answer for each question, and display the final score.
How We Built It
Collection
We created a Questions collection that stores all of the questions and the visitors' answers.
Page Elements
We added the following elements:
3 text elements: for welcoming site visitors and showing the score.
2 buttons: start quiz button and submit button.
Repeater: contains the questions and their possible answers. After submission the repeater also contains the correct answer for each question and text indicating whether the visitor’s answer was correct or incorrect.
Backend Code
We added the questions.jsw web module to get information about the questions and answers from the Questions collection:
getAllQuestions(): a function that queries the collection and returns all of its items.
getRandomQuestions(): a function that receives a variable indicating how many questions it should return. The function calls the getAllQuestions() function to get all questions and returns the specified number of randomly selected questions.
Page Code
In the $w.onReady function we initiate the buttons' onClick event handlers.
When the site visitor clicks the Start Quiz button we get 5 random questions using the getRandomQuestions() function and bind them to the repeater's data.
When clicking the submit button we first validate that the site visitor has answered all the quiz questions. If so we validate the answers.
We compare each answer with the correct answer we got from the quiz data and show a message letting the visitor know if each answer is correct or not.
We also evaluate the visitor’s score, adding 20 points for each correct answer.
Related Examples
Did this help?
|
Thanks for your feedback!
Maintain Page State
Maintain the state of your repeater while navigating to a dynamic page.
Intermediate
Gift Quiz
Help customers find the perfect gift with a quiz and product recommendations.
Intermediate
Country Autocomplete
Automatically complete country names while typing. Use your keyboard to select the country you want.
Intermediate