Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,810 --> 00:00:06,580
In the previous lecture, we prepped the application by adding in the HTML and CSS.
2
00:00:06,600 --> 00:00:10,950
The next step is to add the logic for making the quiz application functional.
3
00:00:10,980 --> 00:00:13,620
There are a lot of things we want to make functional.
4
00:00:13,650 --> 00:00:15,570
It can seem overwhelming at first.
5
00:00:15,600 --> 00:00:18,990
It's always good practice to break things into smaller pieces.
6
00:00:19,200 --> 00:00:24,240
Let's examine the HTML structure for a moment to decide on how to break things down.
7
00:00:24,270 --> 00:00:29,460
There are two main sections which are at the questions container and result.
8
00:00:29,490 --> 00:00:32,930
It would make sense to put these sections in two separate components.
9
00:00:32,940 --> 00:00:36,320
We'll need to communicate down the data to these components.
10
00:00:36,330 --> 00:00:42,270
It's clear that the whole different types of content one for questions and another for the results.
11
00:00:42,420 --> 00:00:45,660
Let's put both sections into their separate components.
12
00:00:45,660 --> 00:00:47,940
Inside the components directory.
13
00:00:47,940 --> 00:00:53,010
Create two files called questions view and Result View.
14
00:00:55,320 --> 00:00:59,040
We'll add the template and script blocks to both components.
15
00:01:09,480 --> 00:01:15,500
We're going to cut the div tag with the class of questions container from the app component file.
16
00:01:15,510 --> 00:01:17,700
This includes all of its contents.
17
00:01:19,820 --> 00:01:24,950
Then we'll paste the code we've copied into the template block of the questions component.
18
00:01:27,380 --> 00:01:31,550
Up next, we'll need to grab the HTML for the result component.
19
00:01:31,580 --> 00:01:33,770
Go back to the app component file.
20
00:01:33,770 --> 00:01:36,440
Cut the div tag with the class of results.
21
00:01:38,750 --> 00:01:42,380
Well, paste this into the template block of the result component.
22
00:01:44,800 --> 00:01:50,440
The first step we'll usually take when developing applications is to break things down into small components.
23
00:01:50,470 --> 00:01:55,870
We've identified that there are sections for the quiz application, there are the questions, and then
24
00:01:55,870 --> 00:01:57,190
there are the results.
25
00:01:57,190 --> 00:02:01,960
We've put them into separate components to make things easier to manage and organise.
26
00:02:02,200 --> 00:02:04,450
The next step is to prepare the data.
27
00:02:04,450 --> 00:02:09,759
After we have the data in hand, we'll want to pass down the data to any components that need them.
28
00:02:09,759 --> 00:02:12,010
Currently, we don't have any data.
29
00:02:12,010 --> 00:02:17,920
The zip file you downloaded in the previous lecture will contain a file called Data Text.
30
00:02:17,950 --> 00:02:21,760
To save some time, I provided you with some initial data.
31
00:02:22,120 --> 00:02:29,050
Copy everything inside this file, then switch over to the app component file inside the exported object.
32
00:02:29,080 --> 00:02:30,820
Define the data function.
33
00:02:33,050 --> 00:02:35,930
We're going to return the object we copied.
34
00:02:38,070 --> 00:02:40,020
We have copied over a lot of data.
35
00:02:40,050 --> 00:02:43,130
Don't worry, it's not as complicated as it looks.
36
00:02:43,140 --> 00:02:47,360
If we minimize some of the items in the object, we'll find two arrays.
37
00:02:47,370 --> 00:02:50,760
We have the questions array and the results array.
38
00:02:50,790 --> 00:02:53,680
The questions array is an array of objects.
39
00:02:53,700 --> 00:02:57,840
Each object has a question and an array of possible answers.
40
00:02:57,870 --> 00:02:59,550
Each answer is an object.
41
00:02:59,550 --> 00:03:03,480
With the answer there's a property called is correct.
42
00:03:03,480 --> 00:03:06,810
The is correct property will tell the application.
43
00:03:06,810 --> 00:03:11,220
If the answer is correct, there are a total of three questions.
44
00:03:11,220 --> 00:03:15,290
Feel free to modify the questions to whatever you like or add more.
45
00:03:15,300 --> 00:03:17,580
We'll leave it at three to keep it simple.
46
00:03:17,820 --> 00:03:22,350
Moving on over to the results array, we'll find another array of objects.
47
00:03:22,350 --> 00:03:23,760
This one is a little simpler.
48
00:03:23,760 --> 00:03:26,850
This time, each object has four properties.
49
00:03:26,850 --> 00:03:32,730
The minimum and maximum properties will be used to help us figure out what results to display.
50
00:03:32,760 --> 00:03:37,080
We'll use it by checking how many answers the user has answered correctly.
51
00:03:37,080 --> 00:03:41,130
In addition, we have the title and description properties.
52
00:03:41,130 --> 00:03:43,410
There are a total of two results.
53
00:03:43,590 --> 00:03:44,940
That's basically it.
54
00:03:44,940 --> 00:03:48,630
We're going to add more data throughout the development of the project.
55
00:03:48,630 --> 00:03:50,220
This is to start us off.
56
00:03:50,220 --> 00:03:52,410
We're going to minimize both objects.
57
00:03:52,410 --> 00:03:58,740
One thing to know is that I'm adding this data to the app component as opposed to their respective components.
58
00:03:58,980 --> 00:04:01,170
This placement is common practice.
59
00:04:01,170 --> 00:04:05,010
You want to add your data to the parent component and pass it down.
60
00:04:05,010 --> 00:04:08,670
That doesn't mean that children components can't have their own data.
61
00:04:08,700 --> 00:04:12,570
Any data deemed necessary should be placed at the top level.
62
00:04:12,900 --> 00:04:18,540
Before we can pass the data, we need to register the components at the top of the script.
63
00:04:18,540 --> 00:04:21,180
BLOCK We'll import both components.
64
00:04:28,850 --> 00:04:34,040
Next we'll create the components property and register both components locally.
65
00:04:39,030 --> 00:04:43,260
We've called the components questions and result respectively.
66
00:04:43,260 --> 00:04:48,060
With the components registered, we're going to load them inside the template block.
67
00:04:54,850 --> 00:04:58,420
We're loading them in where they were previously placed in the template.
68
00:04:58,450 --> 00:05:00,880
This should result in what we had before.
69
00:05:00,910 --> 00:05:02,810
Before we proceed any further.
70
00:05:02,830 --> 00:05:04,630
Let's make sure things work.
71
00:05:04,660 --> 00:05:11,950
If you haven't already start your server with the NPM run dev command in the browser, you will still
72
00:05:11,950 --> 00:05:14,320
see the static content we have before.
73
00:05:14,320 --> 00:05:17,320
We've confirmed that everything is working so far.
74
00:05:17,530 --> 00:05:21,690
The next step is to make it so that both components don't appear together.
75
00:05:21,700 --> 00:05:25,450
It doesn't make much sense for both to appear at the same time.
76
00:05:25,450 --> 00:05:27,160
We want to render the results.
77
00:05:27,160 --> 00:05:33,130
After the user has completed the quiz, we'll make it so that the questions appear first before we display
78
00:05:33,130 --> 00:05:33,940
the result.
79
00:05:34,270 --> 00:05:40,570
First, we'll need to keep track of how many questions the user has answered inside the app component.
80
00:05:40,570 --> 00:05:43,720
We'll create a data property to keep track of that.
81
00:05:43,810 --> 00:05:49,060
Create a property called Questions Answered with a default value of zero.
82
00:05:52,800 --> 00:05:57,030
We'll assume the user hasn't answered any questions inside the template.
83
00:05:57,030 --> 00:06:01,740
BLOCK We're going to apply the the if directive to the questions component.
84
00:06:02,070 --> 00:06:09,090
The condition will check will be the following questions answered less than questions length.
85
00:06:11,270 --> 00:06:16,640
We're saying that if the number of questions answered is less than the number of questions available,
86
00:06:16,640 --> 00:06:18,710
then show the questions component.
87
00:06:18,740 --> 00:06:20,830
Otherwise, show the results.
88
00:06:20,840 --> 00:06:26,600
This won't work yet because we need to apply the V directive to the result component.
89
00:06:28,960 --> 00:06:30,770
Let's review if this works.
90
00:06:30,790 --> 00:06:35,860
Inside the browser, the questions component should be the only component rendering.
91
00:06:38,130 --> 00:06:38,970
It works.
92
00:06:38,970 --> 00:06:43,440
But before we move on, I want to make sure the conditional directives work.
93
00:06:43,470 --> 00:06:44,400
Open the view.
94
00:06:44,400 --> 00:06:48,570
Developer tools will want to view the data for the app component.
95
00:06:48,810 --> 00:06:52,500
We're going to increment the questions answered property.
96
00:06:54,850 --> 00:06:55,480
As I do.
97
00:06:55,480 --> 00:06:59,590
So the questions component will be swamped with the result component.
98
00:06:59,620 --> 00:07:05,050
This which will occur if three or more questions are answered, since that's how many questions we have
99
00:07:05,050 --> 00:07:05,920
in the array.
100
00:07:05,950 --> 00:07:06,570
Great.
101
00:07:06,580 --> 00:07:09,280
We verified everything is working so far.
102
00:07:09,310 --> 00:07:11,740
We're going to reset this back to zero.
103
00:07:14,090 --> 00:07:16,940
We're able to toggle between both components.
104
00:07:16,970 --> 00:07:20,750
The next part is to make the questions component functional.
105
00:07:22,530 --> 00:07:24,400
Let's think about this for a moment.
106
00:07:24,420 --> 00:07:27,110
What should happen when a user answers a question?
107
00:07:27,120 --> 00:07:30,180
How do we keep track of what question the user is on?
108
00:07:30,210 --> 00:07:33,000
How do we display the current active question?
109
00:07:33,030 --> 00:07:35,650
There are a lot of questions we have to answer.
110
00:07:35,670 --> 00:07:38,040
You can tackle this in several ways.
111
00:07:38,070 --> 00:07:41,760
Remember that there's no absolute way to do things in view.
112
00:07:42,150 --> 00:07:46,110
View is very flexible and offers many methods for doing the same thing.
113
00:07:46,140 --> 00:07:50,280
The following is my solution and how I would go about doing this.
114
00:07:50,310 --> 00:07:54,320
First, we have to loop through the questions and render them.
115
00:07:54,330 --> 00:07:57,210
We want to present one question at a time.
116
00:07:57,210 --> 00:08:01,570
Therefore, we'll have to store the index of the current active question.
117
00:08:01,590 --> 00:08:06,090
We'll use the index to determine whether or not a question should be visible.
118
00:08:06,390 --> 00:08:09,930
Afterward, we'll listen for click events on the answers.
119
00:08:09,930 --> 00:08:14,230
If an answer is selected, then we'll check if that is the correct answer.
120
00:08:14,250 --> 00:08:19,340
If it is, we'll keep track of how many questions the user has answered correctly.
121
00:08:19,350 --> 00:08:23,850
We'll need to update the number of questions answered and move on to the next one.
122
00:08:23,940 --> 00:08:26,100
This is the overall process.
123
00:08:28,860 --> 00:08:32,030
If it doesn't make complete sense, then that's fine.
124
00:08:32,039 --> 00:08:37,169
We'll be going through it together when building the quiz app in accordance with the process, we'll
125
00:08:37,169 --> 00:08:38,740
loop through the questions.
126
00:08:38,760 --> 00:08:42,559
The questions component currently doesn't have the questions.
127
00:08:42,570 --> 00:08:46,350
This is because the question data is stored in the app component.
128
00:08:46,770 --> 00:08:49,260
We'll need to pass it down as a property.
129
00:08:49,290 --> 00:08:53,860
Inside the app component will apply an attribute called questions.
130
00:08:53,880 --> 00:08:56,310
This will be bound to the questions array.
131
00:08:58,540 --> 00:09:02,680
We'll need to accept the questions passed down from the parent component.
132
00:09:02,680 --> 00:09:07,000
Switching to the questions component, we'll add the props property.
133
00:09:09,210 --> 00:09:12,560
We won't bother with validation to keep things simple.
134
00:09:12,570 --> 00:09:15,460
If you want to validate the props, then go for it.
135
00:09:15,480 --> 00:09:17,070
We'll add the questions.
136
00:09:17,070 --> 00:09:18,390
Prop to the array.
137
00:09:20,830 --> 00:09:26,650
After providing the questions to the component, we can loop through them inside the template block.
138
00:09:26,650 --> 00:09:30,850
We'll find a div tag with the class of single question.
139
00:09:30,850 --> 00:09:33,370
We'll want to perform a loop on this element.
140
00:09:33,370 --> 00:09:36,160
We'll add the V four directive to it.
141
00:09:38,490 --> 00:09:45,000
We'll set the expression to question in questions where question is the alias theme we'll use for the
142
00:09:45,000 --> 00:09:47,010
question in the current iteration?
143
00:09:49,350 --> 00:09:55,260
To prevent any funny business will bind the key attribute to question q.
144
00:09:57,720 --> 00:10:01,060
This attribute is to make sure nothing strange happens.
145
00:10:01,080 --> 00:10:05,610
The Q property is the property that holds the actual question itself.
146
00:10:05,790 --> 00:10:08,280
It's unique enough for our case.
147
00:10:08,580 --> 00:10:13,350
The last thing we need to do is replace the static text with expressions.
148
00:10:13,350 --> 00:10:19,380
The text inside the div tag with the class of question should be replaced with the expression.
149
00:10:19,380 --> 00:10:20,820
Question q.
150
00:10:23,190 --> 00:10:25,630
The answers are stored in an array.
151
00:10:25,650 --> 00:10:27,860
We'll have to loop through them as well.
152
00:10:27,870 --> 00:10:35,000
Inside the div tag with the class of answers will be another set of div tags with dummy answers.
153
00:10:35,010 --> 00:10:38,250
We'll be using this element to loop through the answers.
154
00:10:38,250 --> 00:10:41,580
It isn't necessary to have so many dummy answers.
155
00:10:41,580 --> 00:10:44,070
We can keep one and remove the rest.
156
00:10:46,310 --> 00:10:53,420
Then we'll add the V for directive to the one we left with the expression set to answer in question
157
00:10:53,420 --> 00:10:54,620
dot answers.
158
00:10:59,120 --> 00:11:02,770
Each object in the array has a property called text.
159
00:11:02,780 --> 00:11:09,050
We'll use the expression answer text to replace the static text inside the div.
160
00:11:11,290 --> 00:11:12,430
I almost forgot.
161
00:11:12,460 --> 00:11:16,120
We should also bind the key attribute inside the loop.
162
00:11:16,120 --> 00:11:19,090
We'll use the text property for its value.
163
00:11:21,410 --> 00:11:24,480
Let's make one final preview before moving on.
164
00:11:24,500 --> 00:11:26,450
Refresh the app in the browser.
165
00:11:28,550 --> 00:11:30,610
View is looping through the questions.
166
00:11:30,620 --> 00:11:33,290
Each question has its answers outputted.
167
00:11:33,320 --> 00:11:39,200
We still need to make it so that only one question appears, but we'll get to that in the next lecture
168
00:11:39,470 --> 00:11:41,810
to review everything we did so far.
169
00:11:41,840 --> 00:11:47,130
We created two components one for the questions and another for the results.
170
00:11:47,150 --> 00:11:52,080
We then passed down the data from the app component to the questions component.
171
00:11:52,100 --> 00:11:56,960
Lastly, we looped through all the questions and answers in our data.
172
00:11:57,260 --> 00:11:59,640
If you've made it this far, then congrats.
173
00:11:59,660 --> 00:12:03,200
We've still got a ways to go, so I'll see you in the next lecture.
16694
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.