Making the quiz functional
Our UI part is now complete and we can proceed to make the quiz functional. We will do this in a few steps. First of all, we will display the data on the page in two columns. Then, we will make the country names draggable. Finally, the list items with the capital names will be made droppable so that we can drop a country name inside a capital. We will also have to ensure that a droppable capital name accepts only the correct country name. Finally, the resetting logic will be built.
Displaying data on the page
Open the quiz.js
file for editing and write the following code:
On the document ready event we call a function named createQuizLayout
which we need to define now.
Here is what the preceding code does:
- We have defined two arrays named
countries
and capitals
. - The
countries
array contains names of 10 countries and the capitals
array contains names of the capitals of the countries defined in the countries array. The names of capitals must be in the same order as their respective countries. - Since we want to display the names of countries and capitals in a random order, we will create two arrays and fill them with list items and shuffle them.
- We started with country first. We declared an array named
arrCountry
. Then, we loop in the countries
array and create a list item with the country name and push it into the arrCountry
array. - The same process is repeated for the
capitals
array.
An important point to note here is that we are giving a data attribute named index
to each list item having a value from 1 to 10. Since we have both the countries and capital names in the same order, index
will be used to match which country belongs to which capital.
After both arrays are populated, we will shuffle them so that the order of countries and capitals becomes random. For this, we will use a simple shuffle
function from the website http://jsfromhell.com/array/shuffle. The shuffle
function is defined as follows:
After calling the shuffle
function on both arrays arrCountry
and arrCapital
, the array elements are inserted in DOM after combining them into a single string using the JavaScript join
function. The elements in the array arrCountry
are inserted in ul
with the id
value source
and those in the array arrCapital
are inserted in ul
with the id value target
.
Open your browser and point it to the index.html
file of the Chapter1
folder now. You will see a page similar to the one shown in the following screenshot:
If you reload the page, you will see that the order of countries and capitals changes each time. This is because shuffling creates a new order for items of both lists.
To make the country names draggable, we will use the draggable
component of jQuery UI. As the name suggests, the draggable
component allows DOM components to be moved around using a mouse. To do this, go to the $(document).ready()
section of our quiz.js
file and call another function named initQuiz
. The $(document).ready()
callback function should look like this now:
Now define the initQuiz
function outside document ready handler as follows:
The preceding code calls the draggable
method of the jQuery UI library. It is being called upon the li
elements of the ul
source, which means it will make all the list items draggable inside the source ul
. Further, we are also giving the draggable
method three options that we need for our application: revert
, revertDuration
, and cursor
. Let's look at these in more detail:
revert
: This decides whether the element being dragged should revert to its original position or not. In our case, we will set it to true
. We will drag a country name onto a capital name and revert it to its original position, that is, the country list. Another possible value for revert
is false
, which means it will stay at the place where it is when dragging stops. The values, valid
and invalid
, can also be provided (as strings) for the revert
option. The value valid
means the draggable object will revert only if the draggable object has been dropped on a droppable element. The value invalid
means the draggable
fuction will revert if the draggable object has not been dropped. Alternatively, a function can also be provided to revert. This is required in complex cases where we need to perform any manipulations. The return
value for this function will decide if it will revert or not. If true
is returned, the element will revert.revertDuration
: This defines the duration for the revert
option in milliseconds. The lower the value, the faster it will revert. This value is not considered if the revert option is set to false
.cursor
: This is the style of cursor while an element is being dragged.
Our draggable elements are ready now, and so it is time to make the capital names droppable and build the logic to match countries to their correct capitals.
Droppable capital names and scoring
In the previous section, we created an initQuiz
function where we made our countries draggable. After the draggable code, write the following code to make the capitals droppable:
Now save the quiz.js
file and refresh your browser. You will be able to drag the country names now. Drag a country name to its correct capital and you will see that the country will revert to its original position. The capital list item will show a bounce effect and its text will change to Correct!. Both the country and capital names will be disabled now. You will not be able to drag the country name as well. On the top left hand side, the page will show the score as 1 points.
The screen will look like the following screenshot:
Try the drag and drop for all countries in the left-hand side list. When you have matched all countries correctly, you will see a dialog box and the page will look like the following screenshot:
So, a lot is happening in the preceding code. We will look at it step by step.
We defined a variable named totalScore
and set it to 0
. We also inserted the score inside the HTML element with the id
value score
. Each time the quiz starts, the score will be reset as well. After this, we call the droppable
method of jQuery UI on the list items of ul
with the id
value target
to make them ready to accept the draggable country elements.
We are using the accept
option of the jQuery UI draggable
method to check for the correct matches of country and capital, and we are using the drop
event to change the UI and scoring.
Accepting a draggable element
The accept
option of a droppable
method defines which draggable element will be accepted by the droppable
method when a draggable element is over it; either a jQuery selector or a function can be provided for this purpose. If a selector is given, only the draggable element matching that selector will be accepted by the droppable
method. Since we want to match an individual country to its capital, it is better for us to use a function instead. The function will receive the current draggable element that is being dragged as a parameter. Inside the function, $(this)
will refer to the current droppable element. The code is as follows:
Since we have already defined data attributes for both countries and capitals, we can match those to check if the current draggable-droppable pair is a correct country-capital pair or not. If the indexes match, we return true
; otherwise, we return false
.
A return value true
means the droppable
method will accept the draggable element, and will allow the draggable element to be dropped in it.
The drop
event will receive a draggable element once it has been passed from the accept
option. If the accept
option returns false
for any draggable element, then the drop
event will not be called. In our case, this means we will only receive a country's draggable element and its corresponding capital's droppable element.
The callback function for the drop
event receives two parameters: event
and ui
. Of these two, we are interested in the ui
object. Among other values, it provides us with a reference to the draggable element that was dropped. To refer to the current droppable element where the draggable element is dropped, we have $(this)
variable with us. The code is as follows:
In the preceding code, we added the jQuery UI framework's CSS class ui-state-highlight
to the current droppable element and then changed that list item's HTML content to Correct! and added the bounce
effect to the droppable capital.
Since the droppable capital has been matched successfully with its country, we no longer need it as a droppable element. Hence, the preceding code uses the disable
method of the droppable component to disable the droppable functionality.
The next two lines add CSS classes named correct
and ui-state-error
to the draggable
method and then disable it. The code is as follows:
The correct
class will be used to determine how many successful countries have been matched. The class ui-state-error
is just for presentation purposes to make the successfully matched country name highlighted. Using the draggable disable
method, we disable the specific draggable element as well, because it has been matched and we do not want it to be dragged again.
Since the drop
event receives only the accepted draggable elements, we can safely increase the variable totalScore
by 1
and insert the new value back to the DOM in the element score
. This shows us the latest score each time a new match is made.
Finally, we count the number of list items in the countries' column that have the CSS class named correct
associated with them. Since we have 10 elements, if all the 10 list items have the CSS class correct
attached to them, it means the quiz is complete. We then show a jQuery UI dialog component that we kept hidden in our HTML page initially.
If you were wondering why we created the functions createQuizLayout
and initQuiz
when we wrote the code without them, the answer is that we need to call them again. It is better not to repeat yourself. We can now reset the quiz without having to reload the page.
We have already created an element with id reset
. Visit the $(document).ready()
callback again and write the following code after those two function calls. The section will now look like this:
We have an event handler registered at the click of the reset
button. It is using the destroy
method of jQuery UI on the draggable and droppable elements. The destroy
method will remove the complete draggable and droppable functionality from respective elements. It will also remove any special CSS classes that jQuery UI might have applied earlier.
After bringing the page to its initial state, we call the createQuizLayout
and initQuiz
functions again, which will initialize our quiz once more.