Buying a Car ?
As one might expect, our car buying function is going to take in some parameters. The price of our old car and price of the new car that we want to buy, naturally. As well as how much we think it is possible for us to save each month and lastly, the depreciation percent by month. Something else that is very important to note is that at the end of every two months, our depreciation rate will grow by half a percentage. This would be a little unrealistic in the real world due to the fact vehicles depreciate differently, but for the purposes of this challenge the depreciation rate will affect both vehicles equally.
I know, I’m personally a car guy but I did not make this challenge, I am only providing my solution. The last and most important piece of information we need before getting started is what our function needs to return. We will need to return the number of months it will take us to save up and buy the new car. As well as how much money we will have left over from the transaction, if any. They should both be returned in an array, respectively. So, buckle up and let’s start creating that function!
First things first, we know that our answer is going to be an array. We also know that array needs to contain the number of months it took us to buy the car as well as the remainder of our savings after the purchase. Right there we have just found two different variables we will need to create, one for months and another for savings. So, let’s do just that.
As you can see, we have currently set our savings and months to both zero, this is because we have just decided that we want to get that new shiny car. Well, now what? Let’s start by creating a loop that will execute until our savings and the price of our old car are greater than or equal to the price of the new car we want to buy. We can achieve this by using a while loop like so.
Now that we have created our loop, we will need to make some additions, or we will forever be stuck in an infinite loop. Not really though, but you get the idea. There are a few things right off the bat that we can add to our loop. We know that each iteration of our loop will represent a month, so we will need to increment our months by one. We also know that each month we need to add our monthly savings to our total savings. Lastly, we also know that our rate will increase at the end of every two months. Let’s take a look at how we can do this.
As you can see, we did just that. We increment our months, add our monthly savings to our total savings, and increment our depreciation rate by half a percentage at the end of every two months. But what exactly is going on here? Well, by using the modulo operator we check to see if our remainder is equal to one. This lets us know that the number is an odd number, but it’s every two months though? Well yes, zero is month one and one is month two. We will increase our depreciation rate by that half percent and will keep doing so every two months. Otherwise, nothing will happen and achieved this all through using a ternary conditional.
This is only half the battle though. We still need to adjust the prices every month on our old car as well as the new car we are looking to purchase. To achieve both this we will simply have to use some good old-fashioned algebra and do some math! I know, not very exciting but I thought the exclamation would give a more positive effect.
As we know our depreciation rate is in a percentage, to use it algebraically, we need to convert it into an actual number. To do so we simply need to divide our depreciation rate by 100. This will turn our percentage into a decimal, we will then need to multiply that decimal by the vehicles value. This value is the amount of money the vehicles worth has depreciated. We then just need to subtract that value from the starting price. This will apply to both vehicles and can be achieved like so.
This now adjusts both our current and future cars price every month and will let us break free of our loop. But something is still missing, oh that’s right it’s our return. Remember we have been tasked with returning the number of months as well as the remainder of our savings in an array. Seems easy enough so let’s do that, and since we want to be returned with whole numbers let’s round our savings appropriately. We can finish off our function like so.
Congratulations everyone we have successfully created our car buying function that returns the number or months needed as well as the remainder of our savings after purchasing our new vehicle! I would like to thank you for reading along and hope that you have learned something new! Keep an eye out for more articles and walkthroughs in the future!
If you would like to view a copy of these examples to play around with for yourself or just for reference, you can find the link for the GitHub Repo here.