WEBVTT 00:00:06.660 --> 00:00:10.720 What we want is to generate a question like this. 00:00:11.190 --> 00:00:18.720 So this is going to be a multiplication operation consisting of two numbers each of them between 1 and 00:00:18.720 --> 00:00:20.110 10. 00:00:20.160 --> 00:00:27.720 So are we going to do we are going to generate a number between 1 and 10 and stories in a variable x 00:00:28.320 --> 00:00:33.540 and then we're going to generate another number between 1 and 10 and store it in another variable. 00:00:33.540 --> 00:00:34.510 Why. 00:00:35.030 --> 00:00:41.550 And then we are going to store the products of x and y inside another variable that we're going to call 00:00:41.790 --> 00:00:42.990 correct answer. 00:00:43.530 --> 00:00:54.180 And then we are going to change the in SML of this box using x and y and this multiplication operator 00:00:54.230 --> 00:00:54.760 . 00:00:55.600 --> 00:01:03.630 And also we are going to place the correct answer in one of these four boxes so will need to choose 00:01:03.720 --> 00:01:06.850 a random number between 1 and 4. 00:01:07.320 --> 00:01:13.230 So if our number is three then we play the correct answer in box number 3 for example. 00:01:13.780 --> 00:01:14.800 OK. 00:01:15.780 --> 00:01:22.620 So in this case for example the correct answer is in box number 3. 00:01:23.010 --> 00:01:27.030 In this case it's box not before or it's. 00:01:27.300 --> 00:01:34.530 And regarding the other boxes we're going to fill them with wrong answers. 00:01:34.530 --> 00:01:34.880 All right. 00:01:34.920 --> 00:01:38.790 Let's start with our variable x. 00:01:39.270 --> 00:01:49.650 So if we use the function random or the method random of month then we are going to get a number between 00:01:49.800 --> 00:01:51.760 0 and 1. 00:01:51.780 --> 00:02:01.070 Strictly less than 1 what we want here is a number between 1 and 10. 00:02:01.770 --> 00:02:09.790 So if we multiply the spy turn then we're going to get a number between 0 and 10. 00:02:09.810 --> 00:02:10.230 Strictly 00:02:14.310 --> 00:02:19.240 if we use the function math dots around. 00:02:19.470 --> 00:02:26.580 If you remember this function is going to around whatever barometer we give to it to the nearest integer 00:02:26.580 --> 00:02:27.660 . 00:02:27.660 --> 00:02:39.510 So for example if we get five points 6 there then the function round is going to return 6 to us if we 00:02:39.510 --> 00:02:47.440 get one point three then it's going to return 1 if we get zero points. 00:02:47.470 --> 00:02:52.080 Four is going to return zero nine point four. 00:02:52.320 --> 00:02:54.560 It's going to return nine. 00:02:55.740 --> 00:03:01.060 Well we want X to be between 1 and 10. 00:03:01.470 --> 00:03:04.380 So it wouldn't accept 0 for example. 00:03:04.440 --> 00:03:05.860 All right. 00:03:06.510 --> 00:03:08.570 So we can use a nice trick here. 00:03:08.760 --> 00:03:13.660 So rather than multiplying this by 10 we can multiply by 9. 00:03:13.680 --> 00:03:23.400 So now we get a number between 0 and 9 and we can add 1 to this number and now we are sure that we are 00:03:23.400 --> 00:03:31.540 getting something between 1 and 10 or at least create another variable y 00:03:34.170 --> 00:03:47.700 also between 1 and 10 and we say that's the other variable which is correct and so is the product of 00:03:47.820 --> 00:03:52.340 x and y. 00:03:56.040 --> 00:04:02.310 Well we're going to need this variable when we click on the boxes. 00:04:02.770 --> 00:04:11.010 And since clicking on a box is going to happen outside this function then we are going to place this 00:04:11.010 --> 00:04:14.920 variable correct answer outside this function. 00:04:15.180 --> 00:04:19.470 So we're going to declare this variable at the beginning of the code 00:04:22.490 --> 00:04:33.500 right and then uses inside this code because otherwise once we go out of this function then we can't 00:04:33.510 --> 00:04:34.710 use this variable anymore. 00:04:34.740 --> 00:04:37.200 If we define is inside the function. 00:04:37.270 --> 00:04:37.820 OK. 00:04:37.960 --> 00:04:38.320 So 00:04:43.750 --> 00:04:49.170 now of course our variable x and are valuable why are we of course the correct answer. 00:04:49.330 --> 00:04:56.880 So we say that now we're going to change to in a Ishmail of these elements to X multiplied by Y. 00:04:57.250 --> 00:04:58.420 So let's do that. 00:04:58.780 --> 00:05:01.710 So this element has caused an ID question. 00:05:02.080 --> 00:05:10.270 What's going to access is using his ID which is question and we are going to change in the Ishmail 00:05:13.210 --> 00:05:21.490 to X and then we add to that the multiplication sign 00:05:25.880 --> 00:05:29.740 and then we asked about Y. 00:05:30.630 --> 00:05:36.790 Just remember when we odd numbers and strings then everything becomes strings. 00:05:36.800 --> 00:05:43.460 Now let's choose a random position where are we going to place our random or our correct answer. 00:05:43.810 --> 00:05:46.970 So are we going to choose a number between 1 and 4. 00:05:47.650 --> 00:05:53.880 So to do that we're going to use the same method we used earlier. 00:05:53.940 --> 00:05:57.700 So our variable is going to be correct position 00:06:05.450 --> 00:06:11.290 and rather than multiplying Mahto random by 9 we multiply by 3. 00:06:11.300 --> 00:06:16.450 So we get a number between 0 and 3. 00:06:16.550 --> 00:06:22.130 And that was we odd one that becomes between 1 and 4. 00:06:24.250 --> 00:06:26.360 Now let's place the correct answer. 00:06:26.440 --> 00:06:32.940 Inside the Box corresponding to the correct position that we have just generated. 00:06:33.110 --> 00:06:41.660 So if we look at our index of HMO file we'll see that all the boxes have ID's box followed by a number 00:06:41.660 --> 00:06:42.530 . 00:06:43.430 --> 00:06:52.820 So if our Carex position is three for example then we want to place the correct answer in Box 3 and 00:06:52.810 --> 00:06:54.960 we're going to use its ID for this. 00:06:55.690 --> 00:07:03.390 So we're going to use documents gets elements by ID. 00:07:04.390 --> 00:07:09.500 So if you want to use Box 3 then the idea is Box 3. 00:07:10.150 --> 00:07:13.100 But we want to use the correct position for this. 00:07:13.570 --> 00:07:21.080 So we're going to use box and we're going to add to that's the position. 00:07:21.110 --> 00:07:24.800 So rather than three we can add the correct position 00:07:27.560 --> 00:07:29.930 just like that. 00:07:30.310 --> 00:07:43.770 And then we are going to change the in in a sample of these elements to the correct answer. 00:07:49.060 --> 00:07:51.160 Let's add a comment here. 00:07:51.720 --> 00:07:58.320 Fill one box with the correct answer. 00:07:58.310 --> 00:08:03.340 All right now we are going to move to the other boxes and we're going to fill them 00:08:06.520 --> 00:08:08.850 with the wrong answers. 00:08:09.740 --> 00:08:18.480 So to do this we are going to cycle through all these boxes and we'll fill them with wrong answers. 00:08:19.060 --> 00:08:25.900 But we are going to check if the box that we're looking at is the initial box as has the good answer 00:08:25.900 --> 00:08:27.280 . 00:08:27.280 --> 00:08:30.820 So to do this we're going to use a for loop. 00:08:31.630 --> 00:08:35.040 So our barometer is going to be I. 00:08:35.620 --> 00:08:40.220 And first of all we are going to check if 00:08:43.510 --> 00:08:46.360 I. 00:08:46.370 --> 00:08:47.330 Is not equal 00:08:49.970 --> 00:08:54.650 to the correct position. 00:08:54.640 --> 00:09:01.610 And then in that case we are going to dinner is a wrong answer and place it in the box. 00:09:01.670 --> 00:09:07.890 So he is going to start at 1. 00:09:09.250 --> 00:09:12.840 And we wanted to be less than five. 00:09:13.290 --> 00:09:18.780 And every time we created by one. 00:09:19.210 --> 00:09:22.230 So I'm going to go from 1 to 4. 00:09:22.690 --> 00:09:32.280 So if he is most equal to the correct position then we're going to do we are going to create a variable 00:09:32.290 --> 00:09:32.400 . 00:09:32.440 --> 00:09:34.310 Wrong answer. 00:09:35.620 --> 00:09:44.750 So this variable is going to be the product of two random numbers between 1 and 10. 00:09:44.750 --> 00:09:45.690 All right. 00:09:46.370 --> 00:09:52.370 So let's use this code. 00:09:56.270 --> 00:09:59.150 Bases in there. 00:10:00.270 --> 00:10:07.200 So it is going to be a wrong answer. 00:10:11.040 --> 00:10:16.270 Now we'll need to place the wrong answer in the box corresponding to the position. 00:10:16.890 --> 00:10:23.440 So to do this we are going to access the books using its ID. 00:10:23.820 --> 00:10:30.940 So the idea of the books is going to be books plus why. 00:10:31.180 --> 00:10:38.820 So if it is true for example that rather than writing books too is going to be books plus too that's 00:10:38.820 --> 00:10:43.560 going to give us books too and they would change the shame of these books 00:10:50.530 --> 00:10:51.260 too. 00:10:51.270 --> 00:10:53.240 Wrong answer. 00:10:54.540 --> 00:10:57.300 So if you save and try this 00:11:00.270 --> 00:11:04.980 now you can see all the boxes filled with answers. 00:11:04.990 --> 00:11:07.080 So that's the correct answer. 00:11:07.260 --> 00:11:11.040 And all the other ones they've got wrong answers. 00:11:11.030 --> 00:11:18.740 Reset the game and try again seeing and again. 00:11:20.310 --> 00:11:26.990 Just one comment before we continue the comparison operator that we use there. 00:11:27.060 --> 00:11:35.150 It means most equal value or most equal type because we're using two equal signs. 00:11:35.160 --> 00:11:42.390 If we use only one equal sign this means most equal value and that's all we need. 00:11:42.790 --> 00:11:44.110 Just a quick review. 00:11:44.120 --> 00:11:46.850 We use two equal signs like this. 00:11:46.870 --> 00:11:56.630 This means equal value and three equal size means equal values and equal types. 00:11:56.670 --> 00:12:03.500 In our case we are looking at the value only and will check whether they are different. 00:12:03.610 --> 00:12:08.000 So it's going to be exclamation mark followed by only one equals nine. 00:12:08.620 --> 00:12:11.580 We are looking at the values. 00:12:11.810 --> 00:12:16.620 Ok so now we want to make sure that's all the wrong answers. 00:12:16.650 --> 00:12:19.510 They are different from the correct answer. 00:12:20.010 --> 00:12:29.250 So to do this before placing a wrong answer inside a box we are going to check that that's a wrong answer 00:12:29.500 --> 00:12:33.050 is different from the correct answer. 00:12:33.060 --> 00:12:39.600 So when we are generating the wrong answer we are going to do that check. 00:12:39.610 --> 00:12:45.090 So first of all we are going to declare our very well wrong answer. 00:12:45.210 --> 00:12:55.350 And then after that we are going to set the variable wrong answer to the random number here but we need 00:12:55.360 --> 00:13:00.490 to check if this wrong answer is equal to the correct answer. 00:13:00.900 --> 00:13:06.510 So if it's equal to the correct answer we will need to do it generates another wrong answer and then 00:13:06.510 --> 00:13:07.230 check again. 00:13:07.440 --> 00:13:10.670 And if it's equal to the correct answer we need to generate another one. 00:13:10.680 --> 00:13:15.990 So we're going to keep generating the wrong answers until we get a number that is different from our 00:13:16.050 --> 00:13:17.600 correct answer. 00:13:17.620 --> 00:13:21.000 So this is reminding us of a WHILE loop. 00:13:21.120 --> 00:13:27.410 So we're going to do going to copy this code and place it inside a while loop. 00:13:27.570 --> 00:13:41.090 So the condition of our while loop is whether the wrong answer is equal to the correct answer. 00:13:41.110 --> 00:13:44.740 So as far as the wrong answer is equals the correct answer. 00:13:44.880 --> 00:13:47.730 We are going to keep generating 00:13:50.800 --> 00:13:51.930 wrong answers. 00:13:52.290 --> 00:13:54.020 OK. 00:13:54.810 --> 00:13:59.550 So if we try this we save and starts our game. 00:13:59.550 --> 00:14:03.970 The problem is does all the wrong answers are going to be undefined. 00:14:04.750 --> 00:14:14.610 And the reason why is from the very beginning our condition here is not verified because we are starting 00:14:14.620 --> 00:14:17.490 with a variable wrong answer. 00:14:17.700 --> 00:14:20.010 Is undefined. 00:14:20.400 --> 00:14:28.510 So we never get here and we never generates a random number for our wrong answer variable. 00:14:28.500 --> 00:14:32.580 So we're going to do rather than using this form of the While loop. 00:14:32.730 --> 00:14:34.090 We're going to use another form. 00:14:34.410 --> 00:14:43.170 So we will start first by executing this code and then checking the condition. 00:14:43.170 --> 00:14:51.070 So to do this we're going to keep our statesman's check in the condition but we are going to generate 00:14:51.150 --> 00:14:54.370 a random answer before the while condition. 00:14:54.530 --> 00:14:58.180 And is going to be using the do statements. 00:14:59.350 --> 00:15:04.240 So that same sex is due followed by curly brackets and then inside the Rockies as we go into space space 00:15:04.260 --> 00:15:07.520 our code. 00:15:08.250 --> 00:15:16.920 Ok so if we save this and refresh then these work in and we can see that we've got our correct answer 00:15:16.920 --> 00:15:17.390 there. 00:15:17.700 --> 00:15:25.460 And we can verify that all the time the wrong answers they are different from the correct answer. 00:15:26.010 --> 00:15:31.650 So we never get a wrong answer equal to the correct answer. 00:15:32.670 --> 00:15:41.340 OK so what's happening here is that we will execute this code at least once and then we do our first 00:15:41.350 --> 00:15:42.880 check. 00:15:44.160 --> 00:15:44.990 OK. 00:15:45.030 --> 00:15:47.460 Now we want something different. 00:15:48.240 --> 00:15:53.600 We want to make sure that all the wrong answers are different. 00:15:54.030 --> 00:15:59.220 So we don't want any to equal wrong answers. 00:15:59.220 --> 00:16:01.840 So there are different ways of doing this. 00:16:02.010 --> 00:16:08.610 The way I'm going to go for is as following. 00:16:08.620 --> 00:16:12.740 I suggest you do your own way and then you can compare to the way it is I will go for. 00:16:12.740 --> 00:16:24.060 So before going inside the for loop I'm going to create an array. 00:16:24.060 --> 00:16:28.130 I'm going to call it answers. 00:16:29.310 --> 00:16:38.230 So this array is going to contain all the answers that we have including the correct answer. 00:16:38.550 --> 00:16:42.870 So it's going to happen is that we will fill this array first with the correct answer. 00:16:43.440 --> 00:16:52.770 And then as we are filling the boxes any new value we generate for one box we're placed inside this 00:16:52.840 --> 00:17:03.900 array and then every time we want to place a value inside a box we will check if that value is in our 00:17:03.960 --> 00:17:07.150 array already or not. 00:17:07.140 --> 00:17:14.310 So let's start first of all the first item we're going to place inside our array is going to be one 00:17:14.430 --> 00:17:16.670 item which is the correct answer. 00:17:17.730 --> 00:17:20.010 And then we're going to go inside our four loop