Hey, everyone, let's now solve the last of the exercises.
So, number four, we should run a loop through this array and then we should print.
Each of the sales inside this table and we should calculate the results, but we have to ignore the
sales if a refund has been requested.
So to get started, let's copy this.
And let's bring it to visuals to record, so now this is.
Number four.
And now we have all this array.
So let's start a loop, so let's use a for loop.
So A is going to be actually, I don't need the VA keyword here, so A is going to be less than Salce.
Not lenth.
So now, in order to make this easier at every iteration of the loop, I'm just going to.
Create a variable called sail, and I'm just going to assign sails.
With an index of a.
Because then in the first iteration of the loop, this is going to go to the sale variable, then this
one, so now it is going to be easier because I won't have to use this notation every time.
I would just use the sale variable.
If you want, you can choose a different name because this may be a bit confusing.
Sale and sales.
You can use a different name if you want.
For me, this is good.
So let's see how that table works.
When we go here, we can see that we have a row, so at the beginning of the loop we are going to start
a row and then we just need to send three T.
D elements with the name, date and amount.
So at each iteration, we are going to start a line and age HTML line.
Which is going to start by opening a table row, then we are going to add.
The tea, the elements, so we are going to add a TD.
So here we just concatenate it.
So now we have.
The table and inside it, we are going to concatenate something so TRD plus the name of the student,
so the name is going to be Sayle, that student.
So this is why I created this variable first, just so I don't have to repeat this all the time.
So Sailboard student.
Then I'm just going to close this.
Now, I'm just going to do the same thing for the date.
And for the amount.
Let's remember that the amount is a number and we are concatenating this with text, so in order not
to do the same mistakes we did before, let's use the two string method.
Now, this is better, and here it's I think this error is because we are using the VA key word, so
let's try to erase this.
So now this is correct.
Now we are just sending the three informations we need.
And now we are just going to finish our line.
By closing the table wrote.
So closing the table wrote, OK, so when we finished doing this.
We can just get that element.
Which is going to be, let's see, the stable, so the tea body.
Is where this is going to be.
So the idea is course sales.
So the inner age TML.
It's going to be plus equal line, so we are concatenating otherwise at every iteration of the loop,
we would just erase what's inside you.
So this is why we need to concatenate or increment the value of this inner HTML.
Pretty cool.
Now, we know that before starting the loop, we can just clean what's inside here.
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.
So before we move ahead, let's see if this is working.
We still need to ignore if there has been a refund.
But for now, let's just test if this is working.
So we should always do stuff step by step.
So refreshing saving this and refreshing the page.
Going down here, we can see that we were able to do the first part, so we are sending these values
to the table, which is pretty cool.
Now, there was a refund.
So here Martin Hayse and Martin is here, so we should not use it.
So now let's fix this.
So now we can just use an if statement.
And this time we can actually we should create the sale variable before we do this test, because now
we are going to test if there is a sale that.
Refund requested.
If there's no refund, this is going to return no, and we know that JavaScript considers null to be
false.
So in this case, we are not going to enter here.
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
and just paste all our code inside the else statement like this?
Because if there has been a refund, then we shouldn't do anything.
Because we are not going to use it in the calculation, we are not going to send it to the table, but
if not, then we are going to run all this.
But this is a bad way of writing and we don't need our statement.
All we need is.
Test the opposite.
So how do we test the opposite?
We use the exclamation mark.
So like I said, this means not so.
I didn't show an example like this before.
So if you want, you can just do sale that refund requested.
Equals, no, you can do it like this, but it's much better to do like this because then we are just
testing the opposite of what we wanted.
So if this does not return false.
Then we're going to do all this, let's see if this is working.
Refresh the page.
And now we don't have Martin Hayes in the list anymore, pretty good, so now we just have to calculate
the total.
So before starting the loop, let's start a variable for the total sales.
That's going to start as zero.
And then at each iteration of the loop.
We are just going to do total sales plus equal.
Sale, that amount.
We already have it as a number, so we don't need to do any conversions, and now this time we should
only print it when we finish the loop, we don't have to replace that value every time.
So when we are finished with the loop, so when we run through all the sales we have, we can just send
the result to that element.
So let's go there.
Let's click here.
We can see that this TRD element has its own ID.
So this is what we need.
And now we can send the total sales to this element.
So let's save this, let's refresh the page.
And now we can see that we are starting to do interesting stuff, if you had problems solving this one
because of the table, I expected you to have some knowledge of age HTML.
But if not, don't worry.
Sometimes students, they they get a bit frustrated because they cannot solve an exercise.
So they they tell me that I did not teach something before sending the exercise.
Well, don't worry too much about this because this video with the solution for this exercise is also
a lesson.
So if you had trouble with this, that's not a problem.
The important is that you see the solution, then you see how to do it.
And now what you can do is just erase all the code and try to redo it this time by yourself.
And then you are going to learn how to do this.
And about the exclamation mark that I've used here, again, you don't have to use it if you don't want,
you can just do it like this if that refund requested.
Equals No.
As you can see.
This is going to work the same way, so again, I just wanted to show you something different.
If you want, you can research more about that operator.
So I hope you like this exercise.
Now, we are starting to do more complex stuff and we still have a few things to learn about JavaScript.
So I'll see you in the next video.