1 00:00:07,420 --> 00:00:13,390 Hey, everyone, let's now solve the last of the exercises. 2 00:00:13,420 --> 00:00:21,730 So, number four, we should run a loop through this array and then we should print. 3 00:00:22,720 --> 00:00:33,040 Each of the sales inside this table and we should calculate the results, but we have to ignore the 4 00:00:33,040 --> 00:00:36,100 sales if a refund has been requested. 5 00:00:36,460 --> 00:00:39,730 So to get started, let's copy this. 6 00:00:41,930 --> 00:00:46,880 And let's bring it to visuals to record, so now this is. 7 00:00:54,460 --> 00:00:55,420 Number four. 8 00:00:58,290 --> 00:01:01,410 And now we have all this array. 9 00:01:05,300 --> 00:01:10,430 So let's start a loop, so let's use a for loop. 10 00:01:18,990 --> 00:01:27,690 So A is going to be actually, I don't need the VA keyword here, so A is going to be less than Salce. 11 00:01:32,150 --> 00:01:34,070 Not lenth. 12 00:01:41,150 --> 00:01:47,480 So now, in order to make this easier at every iteration of the loop, I'm just going to. 13 00:01:49,310 --> 00:01:54,080 Create a variable called sail, and I'm just going to assign sails. 14 00:01:56,580 --> 00:01:58,410 With an index of a. 15 00:02:00,430 --> 00:02:06,820 Because then in the first iteration of the loop, this is going to go to the sale variable, then this 16 00:02:06,820 --> 00:02:12,210 one, so now it is going to be easier because I won't have to use this notation every time. 17 00:02:12,250 --> 00:02:14,950 I would just use the sale variable. 18 00:02:14,950 --> 00:02:20,500 If you want, you can choose a different name because this may be a bit confusing. 19 00:02:20,500 --> 00:02:21,790 Sale and sales. 20 00:02:22,510 --> 00:02:24,300 You can use a different name if you want. 21 00:02:24,550 --> 00:02:26,140 For me, this is good. 22 00:02:26,240 --> 00:02:28,720 So let's see how that table works. 23 00:02:31,630 --> 00:02:39,160 When we go here, we can see that we have a row, so at the beginning of the loop we are going to start 24 00:02:39,160 --> 00:02:44,020 a row and then we just need to send three T. 25 00:02:44,050 --> 00:02:47,590 D elements with the name, date and amount. 26 00:02:48,710 --> 00:02:55,820 So at each iteration, we are going to start a line and age HTML line. 27 00:02:57,840 --> 00:03:05,040 Which is going to start by opening a table row, then we are going to add. 28 00:03:06,520 --> 00:03:11,620 The tea, the elements, so we are going to add a TD. 29 00:03:13,990 --> 00:03:16,480 So here we just concatenate it. 30 00:03:17,950 --> 00:03:18,910 So now we have. 31 00:03:20,220 --> 00:03:27,720 The table and inside it, we are going to concatenate something so TRD plus the name of the student, 32 00:03:27,930 --> 00:03:31,620 so the name is going to be Sayle, that student. 33 00:03:34,320 --> 00:03:39,500 So this is why I created this variable first, just so I don't have to repeat this all the time. 34 00:03:39,780 --> 00:03:41,460 So Sailboard student. 35 00:03:42,610 --> 00:03:45,040 Then I'm just going to close this. 36 00:03:56,850 --> 00:03:59,970 Now, I'm just going to do the same thing for the date. 37 00:04:06,210 --> 00:04:08,130 And for the amount. 38 00:04:10,160 --> 00:04:18,050 Let's remember that the amount is a number and we are concatenating this with text, so in order not 39 00:04:18,050 --> 00:04:24,530 to do the same mistakes we did before, let's use the two string method. 40 00:04:25,540 --> 00:04:34,300 Now, this is better, and here it's I think this error is because we are using the VA key word, so 41 00:04:34,300 --> 00:04:36,050 let's try to erase this. 42 00:04:36,820 --> 00:04:38,400 So now this is correct. 43 00:04:38,740 --> 00:04:42,400 Now we are just sending the three informations we need. 44 00:04:46,900 --> 00:04:49,960 And now we are just going to finish our line. 45 00:04:51,320 --> 00:04:54,800 By closing the table wrote. 46 00:04:57,270 --> 00:05:02,280 So closing the table wrote, OK, so when we finished doing this. 47 00:05:05,970 --> 00:05:08,160 We can just get that element. 48 00:05:09,690 --> 00:05:14,640 Which is going to be, let's see, the stable, so the tea body. 49 00:05:16,550 --> 00:05:18,230 Is where this is going to be. 50 00:05:18,260 --> 00:05:21,650 So the idea is course sales. 51 00:05:26,980 --> 00:05:30,100 So the inner age TML. 52 00:05:32,200 --> 00:05:41,350 It's going to be plus equal line, so we are concatenating otherwise at every iteration of the loop, 53 00:05:41,560 --> 00:05:43,780 we would just erase what's inside you. 54 00:05:43,820 --> 00:05:52,650 So this is why we need to concatenate or increment the value of this inner HTML. 55 00:05:52,750 --> 00:05:53,530 Pretty cool. 56 00:05:53,560 --> 00:06:01,180 Now, we know that before starting the loop, we can just clean what's inside here. 57 00:06:01,840 --> 00:06:10,160 So we've done it a few times and we know why we should do this just to erase this line that I left here. 58 00:06:10,600 --> 00:06:13,690 So before we move ahead, let's see if this is working. 59 00:06:13,690 --> 00:06:17,800 We still need to ignore if there has been a refund. 60 00:06:17,800 --> 00:06:21,300 But for now, let's just test if this is working. 61 00:06:21,550 --> 00:06:24,900 So we should always do stuff step by step. 62 00:06:25,270 --> 00:06:29,350 So refreshing saving this and refreshing the page. 63 00:06:30,920 --> 00:06:39,080 Going down here, we can see that we were able to do the first part, so we are sending these values 64 00:06:39,080 --> 00:06:40,940 to the table, which is pretty cool. 65 00:06:41,630 --> 00:06:43,580 Now, there was a refund. 66 00:06:45,400 --> 00:06:51,020 So here Martin Hayse and Martin is here, so we should not use it. 67 00:06:51,640 --> 00:06:53,260 So now let's fix this. 68 00:06:56,550 --> 00:06:59,670 So now we can just use an if statement. 69 00:07:00,760 --> 00:07:09,310 And this time we can actually we should create the sale variable before we do this test, because now 70 00:07:09,310 --> 00:07:12,310 we are going to test if there is a sale that. 71 00:07:15,320 --> 00:07:17,210 Refund requested. 72 00:07:18,210 --> 00:07:26,580 If there's no refund, this is going to return no, and we know that JavaScript considers null to be 73 00:07:26,580 --> 00:07:27,190 false. 74 00:07:27,420 --> 00:07:31,560 So in this case, we are not going to enter here. 75 00:07:31,890 --> 00:07:39,060 So now how do we write it in a way that we don't need to do it like this and just leave it blank here 76 00:07:39,300 --> 00:07:44,700 and just paste all our code inside the else statement like this? 77 00:07:49,430 --> 00:07:54,110 Because if there has been a refund, then we shouldn't do anything. 78 00:07:55,210 --> 00:08:00,370 Because we are not going to use it in the calculation, we are not going to send it to the table, but 79 00:08:00,370 --> 00:08:03,010 if not, then we are going to run all this. 80 00:08:03,160 --> 00:08:06,940 But this is a bad way of writing and we don't need our statement. 81 00:08:07,270 --> 00:08:08,500 All we need is. 82 00:08:09,770 --> 00:08:10,860 Test the opposite. 83 00:08:10,880 --> 00:08:12,690 So how do we test the opposite? 84 00:08:12,860 --> 00:08:16,280 We use the exclamation mark. 85 00:08:16,430 --> 00:08:20,510 So like I said, this means not so. 86 00:08:20,520 --> 00:08:23,370 I didn't show an example like this before. 87 00:08:23,750 --> 00:08:29,720 So if you want, you can just do sale that refund requested. 88 00:08:30,820 --> 00:08:38,390 Equals, no, you can do it like this, but it's much better to do like this because then we are just 89 00:08:38,400 --> 00:08:40,980 testing the opposite of what we wanted. 90 00:08:48,920 --> 00:08:51,800 So if this does not return false. 91 00:08:55,200 --> 00:08:59,720 Then we're going to do all this, let's see if this is working. 92 00:09:04,790 --> 00:09:06,230 Refresh the page. 93 00:09:08,820 --> 00:09:16,260 And now we don't have Martin Hayes in the list anymore, pretty good, so now we just have to calculate 94 00:09:16,260 --> 00:09:17,100 the total. 95 00:09:18,390 --> 00:09:25,320 So before starting the loop, let's start a variable for the total sales. 96 00:09:27,700 --> 00:09:29,680 That's going to start as zero. 97 00:09:31,860 --> 00:09:34,710 And then at each iteration of the loop. 98 00:09:36,880 --> 00:09:41,380 We are just going to do total sales plus equal. 99 00:09:45,320 --> 00:09:46,970 Sale, that amount. 100 00:09:50,000 --> 00:09:57,500 We already have it as a number, so we don't need to do any conversions, and now this time we should 101 00:09:57,500 --> 00:10:02,860 only print it when we finish the loop, we don't have to replace that value every time. 102 00:10:02,870 --> 00:10:10,310 So when we are finished with the loop, so when we run through all the sales we have, we can just send 103 00:10:10,310 --> 00:10:11,750 the result to that element. 104 00:10:11,770 --> 00:10:12,800 So let's go there. 105 00:10:13,190 --> 00:10:14,270 Let's click here. 106 00:10:14,480 --> 00:10:18,800 We can see that this TRD element has its own ID. 107 00:10:18,800 --> 00:10:20,150 So this is what we need. 108 00:10:40,020 --> 00:10:43,860 And now we can send the total sales to this element. 109 00:10:47,000 --> 00:10:49,910 So let's save this, let's refresh the page. 110 00:10:54,850 --> 00:11:02,710 And now we can see that we are starting to do interesting stuff, if you had problems solving this one 111 00:11:02,710 --> 00:11:08,160 because of the table, I expected you to have some knowledge of age HTML. 112 00:11:08,290 --> 00:11:10,410 But if not, don't worry. 113 00:11:10,420 --> 00:11:16,390 Sometimes students, they they get a bit frustrated because they cannot solve an exercise. 114 00:11:16,660 --> 00:11:22,600 So they they tell me that I did not teach something before sending the exercise. 115 00:11:22,870 --> 00:11:29,590 Well, don't worry too much about this because this video with the solution for this exercise is also 116 00:11:29,590 --> 00:11:30,340 a lesson. 117 00:11:30,520 --> 00:11:33,910 So if you had trouble with this, that's not a problem. 118 00:11:34,090 --> 00:11:38,620 The important is that you see the solution, then you see how to do it. 119 00:11:38,800 --> 00:11:45,340 And now what you can do is just erase all the code and try to redo it this time by yourself. 120 00:11:45,340 --> 00:11:47,950 And then you are going to learn how to do this. 121 00:11:48,430 --> 00:11:57,040 And about the exclamation mark that I've used here, again, you don't have to use it if you don't want, 122 00:11:57,430 --> 00:12:02,590 you can just do it like this if that refund requested. 123 00:12:05,340 --> 00:12:06,450 Equals No. 124 00:12:07,910 --> 00:12:08,930 As you can see. 125 00:12:12,830 --> 00:12:18,320 This is going to work the same way, so again, I just wanted to show you something different. 126 00:12:18,650 --> 00:12:22,790 If you want, you can research more about that operator. 127 00:12:23,150 --> 00:12:25,770 So I hope you like this exercise. 128 00:12:25,790 --> 00:12:33,280 Now, we are starting to do more complex stuff and we still have a few things to learn about JavaScript. 129 00:12:33,290 --> 00:12:34,960 So I'll see you in the next video.