### JavaScript basics pt. 6 - More Math!

JavaScript##### Transcript

English (Auto-generated)

welcome everyone to javascript basics apart six. So last time we talked about some basic mathematical operators that you can use in javascript today we're going to go over some more slowly advanced ones. So let's go ahead and dig right into it. So the first one we have up for today is the remainder operator. The remainder operator is used when you want to divide two numbers and you want the remainder that's left over from the operation and the operator itself is going to look like this percent sign right here, let's go ahead and give an example of that. So again, if you don't have it open already, you'll want to head to replicate dot com if you already have that previous project open, previous front and project with html. CSS and javascript open, you can just head straight to that script dot Js file. If you want to start a new one, you can again just start a new project with html CSS and javascript then just head straight into the script file. Alright, so let's go ahead and use this operation. So just to see the results through console log. And so let's say let's get the remainder of 10 divided by three. So let's do 10 divided by seven. Alright, so we can figure this out because 10, 7 does not divide or you cannot divide 10 by seven evenly, there's going to be leftover number so you can see that leftover number is going to be three. So let's hit run yep, that is our answer. So that is how that works um that's how that works when there is a remainder, if there's not a remainder, so let's say 10 divided by five, five goes into 10 easily, there's not going to be nothing left over, so a result should be zero and it is zero. Alright, so the remainder operator can also be used to check if the number is odd or even and you can do this by dividing the number by two, so if the number is zero, it's even if it's anything else, it's odd. So let's head into that example, I just showed you what it looks like when you divided a number by something that gave no remainder um and there's a function you can do for this, but that's going to be a exercise later on down the line and a few lessons once we've actually dug into functions and uh if else statements, but you know, that's for a few lessons down the road, but so let's say if you divide a number by two and there's no remainder than it, Zero, and then let's see, so we've got sorry, I can't walk across my keyboard while I'm recording this, let's go ahead and divide a on number by two. So we're going to get a remainder of one there and so that's how you know, obviously you can know just by looking at it, but once we start diving more into javascript once you get more familiar with it and getting into functions and logic, you will see that there will be uses for things like the remainder operator, it seemed to recover everything, yep. Yes we did. Alright, so the next one is the argument id addition operator. The augmented addition operator can do a mathematical operation and assign the result of that operation to a variable in one step. So it looks like this right here. So it was just a plus with an equal sign right next to it. And where would this be used? Alright, let's head back over to replicate it. Let's go and clear our console, clear the file. So let's say for example we've got this variable here right at lex we'll call it my bar let's say equals three. Well let's say you want to adjust what's been assigned to my bar to type up my bar here equals because you want to assign something to it and you want to add to to whatever my var already is. So dry that out without using the augmented additional operator, it looks like this. So my var plus two. So you've got this right here, my var equals my var plus two. And so that will we're not actually uh bringing anything to the console. So let's go ahead and put that out so you can see but so since my verse three my verse three equals its value plus two, two. So instead of that you can use the augmented addition operator to shorten that up so that is going to look like this. So instead of having my very equals my via plus two, you can do my var plus equals two and just show you the results gonna be something different. Let's change what my heart is. So my brain articles for let's go ahead and run that. It will now give us six. So there I am. So that shortens up that little bit of code right there. You get the same results in less steps and the space had confusing but it is going to be very useful. So why is this helpful? Why, you know, cut out a few characters, shorten the line of code in javascript and pretty much all programming. If there's an easier or shorter way to do something, you should do it that way shorter, cleaner code is always the goal. Um But you know what, you're not going to know the best way to write code, you're not gonna know the most efficient way to write code. Um and you might not know all the operators all the different ways or you just might forget, you might forget that. There may be a shorter way to do something. So don't stress, you will get there all the time. There's something called re factoring code and people will do that a lot with their projects. Re factoring means it basically means going into a just something um fix it, adjust some of the code and so you may find yourself re factoring old projects you do where you go in and you say oh I know a function I could write for this piece that would shorten it. Or maybe I don't need to write all this out because I found a way to uh I found a way to write all this out in a shorter way because I know this function now. So again, do not stress you will get there, don't worry about your code being the most elegant or efficient right now you will get there. But it's always important to keep these different operators and these different things in mind because you know how you practice it will affect how you do it in the future on your own. So you can also use the augmented additional operator on strings. Let's go ahead and give that a shot here estuh still well name will declare a new string. Our new variable will name its str George for sharing let's say hello a lot of space. That is the name or that is the string that's been assigned to our variable and now we can do string plus equals Right, so we're using the augmented addition operator to do plus equals two world. So we can shorten that out. Let's go and console dot log what string will say now. And that gives us hello world. So that will be useful with string concatenation. That can shorten things down if you need to add things onto it later after you've declared it. So next up we have augmented subtraction and you may guess but it works exactly like augmented edition except you have the minus sign accepted the plus sign. So let's go ahead and try that out. Let's deal into another my var. So we'll declare a new variable with let let my var equal 10. And instead of writing out my of R equals my of R minus whatever we will use augmented subtraction. So my Vaart minus equals nice but not 90. We could do 90 but I'm not going to so we will print out my bar and send it to the console so you can see that message. So what? So my of R minus equals one. So it's 10 minus one. Or sorry 10 minus nine which is one. And that is now assigned to my viral with uh augmented subtraction operator. Mhm. And you might be able to guess what the next to our but we've got augmented and multiplication so works the exact same as the others but with multiplication this time it's good that let's go ahead and give that a shot. So again we'll declare my var with let you can name your variable whatever you want. Six and then we'll do my bar time is equal. So augmented multiplication eight. So you're multiplying eight by eight And exciting that results to my bar console dot log in my bar. And that gives you 64. All right. And drumroll coming up next, augmented division. So again works the exact same as the others. This time with the forward slash for division in front of the equals let's give that one a shot. So declare another variable with let let my var equals 100. So now we do augmented division forward slash and equals let's divide it by 25. Now we will send the result of that. I meant to division operation to the console and see what we can we get four. So that is how that works. So augmented addition, subtraction, multiplication and division all work the same as a regular operators. It just allows you to assign the result of that to a pre existing variable. And again, this may sound like one of those situations where you're wondering when will I ever really need to use this? But you will you will use this uh when you are writing your own code and again, whatever you can do to make shorter cleaner code you'll want to do. And this is one of those situations. So keep those augmented operators in mind. And that is all for this lesson. I will see you in the next one and we are going to dive into a new type of data type called a race. All right. You guys take care