1 00:00:09,120 --> 00:00:16,110 All right, so continuing with our third list of exercises, we were just about to start solving number 2 00:00:16,110 --> 00:00:23,010 three, so this is going to be more or less similar to the first exercise because we can only start 3 00:00:23,010 --> 00:00:27,090 doing anything after pressing the calculate button. 4 00:00:27,270 --> 00:00:31,100 So this button has the idea of calculate. 5 00:00:31,260 --> 00:00:32,970 So let's go back there. 6 00:00:35,200 --> 00:00:38,290 And let's just get this cold. 7 00:00:41,000 --> 00:00:49,400 And based it down here, this time, the baton is going to be the calculate button, then we are going 8 00:00:49,400 --> 00:00:50,870 to start a function. 9 00:00:52,570 --> 00:00:57,580 And here we are just going to get the two grades and the absences. 10 00:01:01,010 --> 00:01:05,440 So grade one, let's go back there to see yds. 11 00:01:07,650 --> 00:01:11,940 So this is grade one, grade two and absences. 12 00:01:12,870 --> 00:01:15,720 So the idea is grade one. 13 00:01:16,760 --> 00:01:18,370 Now, let's do the same thing. 14 00:01:23,090 --> 00:01:24,410 For a grade two. 15 00:01:27,200 --> 00:01:28,340 And for the. 16 00:01:29,600 --> 00:01:30,290 Absents. 17 00:01:37,540 --> 00:01:44,740 Now, before we move ahead, we should test if all of the information have been entered, so let's start 18 00:01:44,740 --> 00:01:54,760 an if statement and this time we're going to use the OR operator, because if grade one is an empty 19 00:01:54,760 --> 00:01:55,360 string. 20 00:01:56,840 --> 00:02:00,830 Or great, too, is an empty string. 21 00:02:03,570 --> 00:02:04,140 Or. 22 00:02:06,990 --> 00:02:09,600 Absences is an empty string. 23 00:02:11,120 --> 00:02:19,040 So if by using the or operator, if one of these tests return true, we're going to enter here. 24 00:02:19,100 --> 00:02:25,760 So this means that something is wrong so we can just send an alert saying that. 25 00:02:27,080 --> 00:02:30,290 All fields must be. 26 00:02:31,660 --> 00:02:37,030 Field, so let's just test this real quick, refreshing the page. 27 00:02:38,410 --> 00:02:45,760 Now, if I click calculate, I'm seeing the alert, so nothing is going to happen if I feel this one 28 00:02:45,760 --> 00:02:53,020 and this one and leave the third one blank and click calculate again, nothing is going to happen. 29 00:02:53,230 --> 00:02:56,030 Only when we type all the information. 30 00:02:56,530 --> 00:03:00,930 When we fill all the feud's, then something is going to happen. 31 00:03:01,270 --> 00:03:06,250 So this is what we need to do inside else statement. 32 00:03:06,670 --> 00:03:12,370 So inside here, the first thing we are going to do is create a variable for the average. 33 00:03:14,420 --> 00:03:22,310 So average is going to be inside parentheses grade one plus grade two. 34 00:03:23,680 --> 00:03:25,120 Divided by two. 35 00:03:27,460 --> 00:03:30,700 And another variable for the present. 36 00:03:32,160 --> 00:03:40,170 So the presence has to be higher than 70 percent, which means that we're going to get the total number 37 00:03:40,170 --> 00:03:45,950 of classes, which is 20 subtract by the absences. 38 00:03:46,290 --> 00:03:50,730 So let's say the person had one absence in this case. 39 00:03:51,000 --> 00:03:53,430 This is going to return in 19. 40 00:03:53,730 --> 00:03:56,930 So the person went to the classes 19 times. 41 00:03:57,270 --> 00:04:01,650 So if we divide this by the total number of classes. 42 00:04:04,270 --> 00:04:11,480 Then we are going to have the percentage of presence, just one thing to pay attention. 43 00:04:11,530 --> 00:04:18,670 We should never repeat values like this, because if we are using this for another class, which has 44 00:04:18,670 --> 00:04:27,280 twenty five or maybe 15 classes, then we are going to look throughout our code and replace this information 45 00:04:27,280 --> 00:04:27,870 everywhere. 46 00:04:27,880 --> 00:04:32,320 So to avoid this, we should create a variable for the total. 47 00:04:34,600 --> 00:04:38,560 Classis, this is in this case, this is 20. 48 00:04:39,590 --> 00:04:47,480 So now we can just use the variable here and the variable here, so if this changes to a different number, 49 00:04:47,660 --> 00:04:53,030 we can just change it here and we won't have to be concerned about the rest of the code. 50 00:04:53,660 --> 00:04:54,080 All right. 51 00:04:54,090 --> 00:04:55,040 So pretty cool. 52 00:04:55,200 --> 00:05:01,140 Now, we already have the presence, which should be higher than 70 percent. 53 00:05:01,160 --> 00:05:03,850 So now let's start doing the tests. 54 00:05:04,280 --> 00:05:12,020 But now, before moving ahead, remember, we should always test what we're doing while we are doing 55 00:05:12,260 --> 00:05:19,340 to avoid mistakes, because maybe at the end we just write all the code and if it doesn't work, is 56 00:05:19,340 --> 00:05:23,630 going to be harder to spot what was the error at the end? 57 00:05:23,810 --> 00:05:28,640 So now let's just check if the average and presents calculations are correct. 58 00:05:29,060 --> 00:05:30,920 So let's do console. 59 00:05:34,730 --> 00:05:36,950 That log for the average. 60 00:05:43,330 --> 00:05:45,640 And also for the presence. 61 00:05:47,450 --> 00:05:52,970 So remember, in the beginning of the chorus, I told you that console, that log was our best friend 62 00:05:53,300 --> 00:05:57,100 because we can use it to test our code while we write it. 63 00:05:57,530 --> 00:05:58,730 So let's do this. 64 00:06:03,190 --> 00:06:10,960 Let's refresh the page and let's try eight and six. 65 00:06:12,520 --> 00:06:19,480 And let's try, I don't know, 10 absences, let's hit calculate, let's go to the council. 66 00:06:21,100 --> 00:06:29,080 So now we have a very strange result in the console, JavaScript is telling us that the average between 67 00:06:29,080 --> 00:06:31,480 eight and six is forty three. 68 00:06:31,510 --> 00:06:34,000 So where does this come from? 69 00:06:34,450 --> 00:06:37,780 Well, to test this out, let's just go back here. 70 00:06:39,860 --> 00:06:41,510 And let's. 71 00:06:43,280 --> 00:06:48,220 Get this statement and run it in the console to see what is going on. 72 00:06:49,450 --> 00:06:54,640 So if we do this and press enter, we can see that we are getting a string. 73 00:06:54,730 --> 00:07:04,360 So probably what JavaScript is doing is just at first we are using a plus sign so JavaScript knows what 74 00:07:04,360 --> 00:07:07,510 to do with strings when we use plus signs. 75 00:07:07,690 --> 00:07:11,260 So it is just getting eight and concatenating with six. 76 00:07:11,270 --> 00:07:12,910 So we are getting eighty six. 77 00:07:13,120 --> 00:07:22,210 But when we divide this eighty six string by two, JavaScript starts to get a bit confused and just 78 00:07:22,210 --> 00:07:26,290 tries to do what it thinks is the correct thing to do. 79 00:07:26,410 --> 00:07:33,060 So just convert this string to number and calculate the average. 80 00:07:33,070 --> 00:07:36,560 So eighty six divided by two is forty three. 81 00:07:36,730 --> 00:07:43,780 So as you can see, remember when we spoke about type conversions, I told you that you should always 82 00:07:43,780 --> 00:07:50,190 be careful about this because sometimes we may have unexpected results. 83 00:07:50,320 --> 00:07:52,960 So in this case we are having a number back. 84 00:07:53,230 --> 00:07:58,450 But this number is far from being correct, so we should not forget to do this. 85 00:07:59,580 --> 00:08:06,030 So when we are getting the first grade, we should apply the pass float at first. 86 00:08:13,720 --> 00:08:24,340 So here we have it and also my apologies, because I forgot to do it in the first exercise, so here 87 00:08:24,340 --> 00:08:25,300 it worked. 88 00:08:25,570 --> 00:08:32,370 But this was actually because I was lucky because I should always pay attention and convert these values. 89 00:08:32,380 --> 00:08:33,040 So here. 90 00:08:34,230 --> 00:08:36,920 We should have done it like this as well. 91 00:08:38,560 --> 00:08:45,370 All right, so going down here now, saving this, let's refresh the page first, let's test if this 92 00:08:45,370 --> 00:08:47,260 one is still working, so. 93 00:08:48,420 --> 00:08:54,270 Thirty five degrees Celsius, it's ninety five Fahrenheit, this is fine, and now this one, let's 94 00:08:54,270 --> 00:08:55,350 try the same number. 95 00:08:55,360 --> 00:09:06,510 So eight and six and 10 absences when we heat calculate now we have the correct values, seven and zero 96 00:09:06,510 --> 00:09:07,060 point five. 97 00:09:07,080 --> 00:09:09,180 So now we are safe to move ahead. 98 00:09:09,660 --> 00:09:13,260 So now we have multiple ways of solving this. 99 00:09:13,260 --> 00:09:16,470 We can use and and or operators. 100 00:09:16,470 --> 00:09:18,450 We can do nested conditionals. 101 00:09:18,450 --> 00:09:19,950 It's really up to you. 102 00:09:20,220 --> 00:09:23,160 Just try not to write too many lines of code. 103 00:09:23,400 --> 00:09:29,790 So maybe your solution wasn't the same as mine, but if your solution is working as well, it should 104 00:09:29,790 --> 00:09:30,660 be fine. 105 00:09:30,930 --> 00:09:33,720 So the way I'm going to solve this is the following. 106 00:09:33,720 --> 00:09:41,720 I'm going to start an IF statement and first I'm going to test if the student has failed for both reasons. 107 00:09:41,970 --> 00:09:44,220 So this means that the. 108 00:09:46,160 --> 00:09:47,630 Average is. 109 00:09:49,400 --> 00:09:52,820 Less than six point five. 110 00:09:53,770 --> 00:09:56,680 And at the same time, the presence. 111 00:09:58,890 --> 00:10:02,670 It's less than zero point seven. 112 00:10:02,850 --> 00:10:07,050 So in this case, I know that the student has failed for both reasons. 113 00:10:08,160 --> 00:10:14,480 So I'm just going to start a variable out here just so I don't have to use the VA keyword all the time. 114 00:10:14,910 --> 00:10:16,440 So now without. 115 00:10:19,250 --> 00:10:22,970 It's going to be the student has failed. 116 00:10:24,720 --> 00:10:26,100 Because of. 117 00:10:27,120 --> 00:10:35,390 Insufficient grade and too many absences, if you want, you can try to write a better text. 118 00:10:35,430 --> 00:10:36,930 This is just an example. 119 00:10:37,200 --> 00:10:40,720 So in this case, I know that the student has failed for both reasons. 120 00:10:41,070 --> 00:10:43,800 So now I'm just going to start and else if. 121 00:10:47,510 --> 00:10:56,090 So this time I'm just going to repeat one of these tests so I know this sounds a bit counterintuitive, 122 00:10:56,090 --> 00:10:57,770 why am I repeating this? 123 00:10:58,010 --> 00:11:04,160 This is just because we know that if this returns true, so both are true. 124 00:11:05,000 --> 00:11:11,200 We are going inside here and JavaScript is just going to ignore all the rest of the structure. 125 00:11:11,450 --> 00:11:16,550 So if we are getting to this point, it means that one of these returned. 126 00:11:16,550 --> 00:11:17,050 True. 127 00:11:17,300 --> 00:11:25,190 So if the average is less than six point five, it means that the presence was good because otherwise 128 00:11:25,340 --> 00:11:26,780 we would have stopped here. 129 00:11:27,020 --> 00:11:28,880 So in this case, the result. 130 00:11:32,720 --> 00:11:38,000 It's going to be the student has failed because of insufficient grade. 131 00:11:40,910 --> 00:11:46,880 So, again, how do we know that it wasn't because of both reasons, because if so, we would have stopped 132 00:11:46,880 --> 00:11:51,200 here and JavaScript would have ignored all the rest of the structure. 133 00:11:52,380 --> 00:11:59,190 So now just another else, if that's going to be very similar, but this time we are going to use the 134 00:11:59,190 --> 00:11:59,940 presence. 135 00:12:00,420 --> 00:12:01,950 So if we fall here. 136 00:12:06,720 --> 00:12:10,470 I just forgot the semicolon here and here aswell. 137 00:12:14,840 --> 00:12:20,720 So if we fall here, it means that the student has failed because of too many absences. 138 00:12:23,780 --> 00:12:28,850 How do we know it, because if it was because of both reasons, we would have stopped here. 139 00:12:30,770 --> 00:12:33,890 Then if we don't go inside any of these. 140 00:12:34,820 --> 00:12:40,130 If statements, we know that the student has been approved, so result. 141 00:12:42,590 --> 00:12:43,670 It's going to be. 142 00:12:46,830 --> 00:12:53,310 This student has been approved now the only thing we need to do is just. 143 00:12:54,580 --> 00:12:57,250 Printing this value inside that element. 144 00:12:57,280 --> 00:13:00,220 So let's check what is that element? 145 00:13:03,240 --> 00:13:05,760 This one has the idea of result. 146 00:13:16,490 --> 00:13:20,930 So now inside it, we are just going to print the result. 147 00:13:25,030 --> 00:13:28,830 Now, let's make some tests to see if this is working. 148 00:13:32,960 --> 00:13:36,980 Now, let's try five, five and 10. 149 00:13:37,010 --> 00:13:43,430 So the student is going to fail for both reasons, let's test this out so now we can see that this is 150 00:13:43,430 --> 00:13:43,970 working. 151 00:13:44,210 --> 00:13:48,440 Now, let's do only two absences now. 152 00:13:48,470 --> 00:13:51,020 It was only because of insufficient grade. 153 00:13:51,350 --> 00:13:52,910 Now let's try 10 again. 154 00:13:53,090 --> 00:13:55,370 But this time, two good grades. 155 00:13:56,920 --> 00:14:04,110 So this time it was only because of the absences of now to good grades and a low number of absences, 156 00:14:04,120 --> 00:14:06,550 and this time this student has been approved. 157 00:14:06,820 --> 00:14:08,920 So this is the way I solved it. 158 00:14:09,430 --> 00:14:15,880 If you did it differently, if you think your way of doing it was more efficient than mine, you can 159 00:14:15,880 --> 00:14:18,520 just share it in the Q&A section. 160 00:14:18,880 --> 00:14:19,890 Let's move on. 161 00:14:20,440 --> 00:14:21,970 There's one thing we didn't do. 162 00:14:21,970 --> 00:14:23,470 We did not validate this. 163 00:14:23,620 --> 00:14:25,090 These numbers like. 164 00:14:26,450 --> 00:14:33,260 Grades from zero to 10, absences from zero to 20, if I try 55 here. 165 00:14:34,170 --> 00:14:40,320 Our program is just accepting this, but I'm not going to go into this now because this video is getting 166 00:14:40,320 --> 00:14:46,350 a bit too long, if you want to try to do this validation, you can do it just as an extra challenge 167 00:14:46,560 --> 00:14:46,980 later. 168 00:14:46,980 --> 00:14:49,920 And of course, we might talk about data validation. 169 00:14:49,920 --> 00:14:52,370 So let's just leave it like this for now. 170 00:14:52,890 --> 00:14:58,870 And now, number four, I honestly spent a bit more time than expected with number three. 171 00:14:59,130 --> 00:15:01,500 So now let's stop this video. 172 00:15:01,680 --> 00:15:05,930 And in the next part, we are going to solve number four. 173 00:15:06,060 --> 00:15:06,840 I'll see you then.