Hi everybody, welcome back. It's time for another code with me. This is one of those situations where I really hope that you put down whatever else you're doing, and really focus on writing your own code from scratch. I know it's easier when I provide the code for you and you follow along, but the real learning happens, when you make the mistakes that come along with coding by yourself. So, let's think about some of the things we've been learning, and how you can incorporate them into a more complex situation. So for instance, can you identify where you need decision points? Up until now I've given you the code. But if I were to present you with new situations, can you modify it in different ways? And when you do modify it, are you giving the very best answer for as many situations as possible? So in today's example, we're going to build on the previous code where we took the average of an array. But now, we wanna make our code a little bit better and a little bit smarter. So let's get started. So here's the code we had earlier in this course. But I've made one very important change, it's right up here In the array. Instead of specifying a value for every single element, I've left some of them empty, or what we in JavaScript call undefined. So the problem is now, when our code goes through and it adds every single element together to come up with the sum We get an error. If you check right down here. Your screen is going to say NaN. That stands for not a number, and it's something that may happen to you quite frequently when you're first coding, if you haven't thought about the logic very much. And it almost always happens when you're doing some math. So, let's think about how we can fix this, and what kind of logic we need behind it. The first place I'm gonna look is right here. And my reason is, we wanna make sure that we're not adding everything into the sum, just the things that truly belong there. Only the things that are numbers. So, if you had to stop and go forward by yourself. The question might be. What do I need to add here? Do I need to add another assignment statement? Do I need to add looping? Do I need to add a conditional? Well, the answer is you wanna add an if statement, because we only want this line of code to happen sometimes. So, let's think about what that would be. Right in here, one line above, I'm going to add an if statement. And this if statement says, you know what, I only wanna do this, if the index has actually been defined, if this is a real number. If it's undefined, which is a very specific word to JavaScript, then skip this. Don't do it. And hopefully right away you can see, that we now have a valid number as our answer. So we're off to a really great start. However, we want to make sure we don't stop right here. That'd be a really bad idea, the reason is, because our answer isn't actually correct. If you look at our array, we have 2 + 5 is 7, 9 is 16, 24, 32. So it adds up to 32 and we have about one, two, three, four, five, five numbers here. So we would expect our average to be something around 6. And that's not the case. We ended up with four. So where did we go wrong? Again it's not a syntax error. It's a logic error. And the problem is right here. We said add up all those numbers, and then divide by the length of the array. But we don't actually wanna do that, because there are a lot of empty elements up there. So let's change this, so that we're actually keeping track of how many elements are good, and only divide by them. Let's get started. So the first thing I'm going to do is I'm going to add a new variable called count. What is this variable gonna do? It's going to keep track of all the good values. So, when I go down here, every time I add a new number into the sum I wanna say oh, that was a good one. Let's add one to count. Now this is very important. I want you to look so closely at this code, and notice that these two lines right here line up. They're kinda right underneath each other. This is your editor trying to give you some sort of information, and what it's saying is, you know what? This count right here this line, it's not part of the if statement, it's not there. And even if I go in, and I say oh, I want it to be part of the i statement so I'm going to do one, two, three oh good, now it lines up. That's not good enough. If you want two things to happen as part of your if statement, you have to remember to put in that extra curly bracket, because that says, oh, I want you to do both of these things. So now we've done this, we're adding one to our account. We have one last place we need to change our code, and that's right down here. Instead of dividing by the length. We're going to divide by count. And there you can see, we've updated our answer and now we have 6.4, which is much closer and hopefully correct for what we were aiming for. So the real lesson behind this lecture today, is not about dividing and it's not about if statements. It's about you going in and thinking logically about your code. And trying to write the best code for every possible, or as many possible scenarios. It's really one of those things that's going to help you solidify your knowledge. So go in there, change the array, try to think up other situations, and practice, practice, practice. Good luck.