21  Coding Challenge 3 Solution

21 Coding Challenge 3 Solution



*** The #1 bestselling JavaScript course on Udemy! ***

“Excellent course. Jonas explains the core concepts in javascript that are usually glossed over in other courses. And he does it in a manner that is clear and concise.” — John Collins

Do you want to learn the number #1 programming language that powers the internet?

Are you tired of wasting your time and money on random youtube videos or JavaScript courses that are either too simple, or too difficult to follow?

Or are you struggling to deeply understand and apply JavaScript to real projects?

If your answer is a big YES… Then this is exactly the course you are looking for!

so I really hope that you gave the challenge to try and of course that you managed to do it so here goes my solution in case you're wondering how I did it I'm going to start by creating the tip calculator function okay so function and I'm going to call it tip calculator then in here you pass a value which I call the bill and then we have our function body okay now we want to depending on the bill value to determine the percentage of the tip that John likes to pay okay so here we have our rules again so 20% if the bill is left in $50 50% if it's between 50 and 200 and then 10% if the bill is more 10 200 so I'm going to define var percentage here and then I'm going to leave it and now in an if-else statement is where I'm actually going to define the the value for the percentage depending on the bill so if the bill is less than 50 then the percentage should be 20 or actually it should be point two so we could write 0.2 but we don't even need to zero here so just point 2 because that's actually the value that later we are going to multiply with the bill okay so remember to calculate 20 percent of a value we multiply it by 0.2 in this case because that's 20 divided by 100 all right so else if the bill is greater or equal to 50 and a of the bill is less than 200 so that's very similar to the stuff that we already did before right and so this should be nothing new for you at this point so in this case or percentage is going to be point 15 and else be percentage will be point zero or actually point one sorry so 0.1 which is 10% okay and in this case we don't even have to specify the if here because if the bill is not less than 50 and not between 50 and 200 well then that means that it's more than 200 and so this case here covers all of that okay and so all we have to do now is to return this percentage times that bill okay and so for example if our bill was $100 then we would divide it with point 15 which would then be $15 and we would return fifteen dollars from this tip calculator function all right now you could of course have done this differently for example you could have done a switch like we did before for example where is it so something very similar to I'm not sure where I am so here is it so you could have done something similar to this one here for example as well where in each case you would then return the bill times the the current percentage yeah but it works fine just like this okay so just to tell that there are always different solutions for the same problems okay and now I just want to actually a test is here with some value so tip calculator and let's set it to 100 and we need to console out log it so again this is just for testing to see if our function is actually working just fine so this you should return 15 right and indeed it does okay so that's 15 percent of 100 which is between 50 and 200 okay now let's try it with 300 and we're going to take 10% of that which should be 30 so let's test and indeed it works and now let's put let's say $10 which is of course less than 50 and so 2% of that should be $2 and yeah indeed it is 2 and so that means that or tip calculator is actually working so all we have to do now is to create these two arrays that John wants as a result so the first thing I'm going to do is to actually store the bills into an array so we have 124 we have 48 and then 268 I believe let's just confirm that and yeah so these are the that we have and then let's calculate the tips actually so the first tip is calculated by using tip calculator with the built on position number zero right then the second one and actually let's put it in a separate line here so the second one is tip calculator builds on element one and then finally the last one is calculating the tip with builds at position number two okay so we just created an array here which contains teeth three values which all come from the result of calculating the tip using our tip calculator function all right so let's login to the console here and so yeah here we have our tips now this here is something weird that is happening with the nine point six and zero zero one and this is something weird that JavaScript does with numbers and no one really knows why this is happening so it should just be nine point six and the same is actually happening here with these five nine and a nine nine eight and this of course should be eighteen point six okay but that's nothing we can change that's not like or fault so JavaScript just does these weird calculations here that do not really make sense but so don't don't worry about them okay so this is the first array that we want so the three tips and then finally we won't want containing all three final paid amounts now so that's also very easy let's say final values so another array and well here we have to say bills at position zero plus the tips at position zero and that's it so very simple let's just grab this here and here it's one and here it's two and so this is actually all that we need to do in order to have or final results okay and now you see that actually here now we have this point six and this point six coming from these two so this is really weird to gather it yeah don't worry about it anyway what we really want to see here is that this one hundred forty two point six is really the sum of the original bill which is 124 Plus this 18.6 okay and the same of course for the address so fifty-seven point six is nine point six plus 48 and the same here for this last element and so with that the challenge is completed okay and you could have done it very differently of course for example you could have declared empty arrays in the beginning and then use the push method to add all of these results to the array but again it's not really important that your code is the same as mine okay I cannot stress this enough so what I really want you is to write code and to think about how to solve these problems and if the results that you get are the same as mine well then that's really fantastic so you've come a long way in a section but there's still a couple of more things to learn so let's now move on

Leave a Reply