The challenge this time definitely has a practical application. It is concerned with simplifying the tricky financial considerations involved in buying a car. For example if, like me, you have set your heart on buying a new car, you’ll need to know how long it will take you to save up for it. You already have a car which you know will currently sell for a certain amount – but sadly every month its value decreases. Fortunately, you can save a certain amount each month, plus the car you desire also decreases in value at the same rate as yours! However, this percent of loss increase isn’t always the same month by month. Blimey! That’s a lot to think about.
No worries – we can work something out which will take the stress out of this situation!
Here is the example from the Codewars Kata ‘Buying a Car’ (currency changed from dollars to pounds):
A man has an old car being worth £2000. He saw a second-hand car being worth £8000. He wants to keep his old car until he can buy the second-hand one.
He thinks he can save £1000 each month but the prices of his old car and of the new one decrease by 1.5 percent per month. Furthermore, this percent of loss increases of 0.5 percent at the end of every two months. Our man finds it difficult to make all these calculations.
Can you help him?
How many months will it take him to save up enough money to buy the car he wants, and how much money will he have left over?
This is a fairly basic problem which can be boiled down to increasing the money we have to start with (the value of our old car) month by month with our savings, until it equals or exceeds the value of the car we would like to buy.
The steps involved to do this I should think are:
Here we go (using Javascript):
I got the wrong results. This is due to the fact that if percentage loss goes up every two months, it should be added at the end of every odd number of loop iterations rather than even ones – so it gets added on every two months. So in fact this bit of the code should be:
[pastacode lang=”javascript” manual=”if%20(numOfMonths%20%25%202%20%3D%3D%3D%201)%20%7B%0A%09%09%09percentLossByMonth%20%2B%3D%200.5%3B%0A%09%09%7D” message=”” highlight=”” provider=”manual”/]
Putting it all together we get:
[pastacode lang=”javascript” manual=”function%20carBuyer(startPriceOld%2C%20startPriceNew%2C%20savingperMonth%2C%20percentLossByMonth%0A)%20%7B%0A%09let%20numOfMonths%20%3D%200%3B%0A%09let%20savings%20%3D%200%3B%0A%09while%20(startPriceNew%20%3E%20startPriceOld%20%2B%20savings)%20%7B%0A%09%09savings%20%2B%3D%20savingperMonth%3B%0A%09%09startPriceNew%20-%3D%20(percentLossByMonth%20%2F%20100)%20*%20startPriceNew%3B%0A%09%09startPriceOld%20-%3D%20(percentLossByMonth%20%2F%20100)%20*%20startPriceOld%3B%0A%09%09numOfMonths%20%2B%3D%201%3B%0A%09%09if%20(numOfMonths%20%25%202%20%3D%3D%3D%201)%20%7B%0A%09%09%09percentLossByMonth%20%2B%3D%200.5%3B%0A%09%09%7D%0A%09%7D%0A%09return%20%5BnumOfMonths%2C%20Math.round(startPriceOld%20%2B%20savings%20-%20startPriceNew)%5D%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
So using the example figures, we can assure the man that he will have enough money to buy the car of his dreams in just six months time – plus he’ll have £766 in change…
Now there’s something useful!