Transcript
00:00 Okay. Let's get into it. Ecommerce utility functions. So, our first function is going to be called calculate tax. We're gonna make that, by using the function keyword and then we have a space and then the name and then our yep.
00:14 It's AI is really good at this stuff. So let's just review it. So it's gonna take our amount. We'll, specify the type for that amount is number. Then we have a comma to separate, for the next, parameter, for the next, argument that we accept.
00:32 That will be rate and then it's colon number, to define the type for that argument. And then our return type is going to be a number. So number number number number. Numbers all over the place. And then our return is going to be the amount times the rate.
00:48 So that is going to be our tax. Okay. Great. So then we can yes. Sure.
00:55 Oh, no. It's deleted all of our console logs. Come on, man. There we go. Format price and apply discount.
01:01 Get rid of those logs. Alright. Or the comments on the logs. So then we have our format price. That's gonna be cents.
01:07 It's a number. Returns a string. This is always funny, when we're talking about, dollars, US dollars or or at least the dollar sign is the dollar sign is used in a template literal to, distinguish a an interpolated value or an expression. But But if you actually want to use a dollar in the string, then you end up with this double double dollar thing which is kind of a little bit confusing. But, yeah.
01:31 We're gonna make a template literal. We want this to be formatted as an amount. So we're gonna have, in a USD amount. So that's just the dollar symbol there. And then, we'll start our interpolated values.
01:44 We'll take the number of cents. We'll divide it by a 100 and we use this to fixed function that we're calling on the the number. So I'll I'll take that off because the AI added that. I'll take that off just to show you what the result would look like and why we're using that to fix thing. And then with that, we actually don't need the parenthesis.
02:06 So there you go. Cents divided by a 100. That's gonna be with a dollar sign at the beginning. That's gonna be our formatted version. Okay.
02:14 Great. And then our apply discount is gonna accept a price and a discount price and it's gonna return a number. And that's gonna be price minus the price times the discount percentage divided by a 100 because it's a percentage and, that is gonna be our discounted price. So then down here what we're calling them, it's calculating the tax for a $100, at 8% and that should give us eight if it's correct. And then our format price for 1999, should and that's stored as cents, should result in 19 dot $9.09.
02:47 And then applying a discount on a 100, dollars for a discount percent of 20 should give us a result of 80. Let's save this. And, yeah. There we go. That looks right.
02:59 Awesome. Okay. So, the AI generated this too fixed thing for us if I put that back. That helps us in in some situations. I don't think that we could ever end up unless you had, like, a decimal of cents, which doesn't really make sense.
03:18 I don't know how you could ever end up in that situation. But if you did, maybe you, like, computed this as a percentage of whatever whatever whatever. I don't know. I suppose that could be a problem. You might wanna round up or something.
03:30 But in, in this case, with those percentage of cents, you do end up with all of that. And if you're formatting a price, having those extra decimals doesn't make any sense. So, we can put that too fixed on here and that will fix that right up. Too fixed. There we go.
03:48 Two. And there's that. So that's all that, was about. I think I'll probably leave that in. So if you wanna explore that, that's a built in feature of numbers in JavaScript is this too fixed utility.
04:02 Okay. Great. So now you have learned how to accept arguments, and pass parameters. So arguments, parameters. I use them interchangeably.
04:12 Everybody does, so don't worry about it. But that now you know how to accept arguments in your functions and make your functions a lot more useful. So, yeah, good job on that one.
