Week 2: Buying a Car

Week 2: Buying a Car

The Challenge

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?

A path

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:

  1. Set counter variables for the number of months and the amount of savings. These will both start at 0.
  2. Write a while loop which compares the price of the new car to the value of the old one plus any savings accrued. If for some reason the value of the desired car is less than (or equal to) the existing car, we won’t ever enter this loop.
  3. Within the loop we have to:
    •  add to the savings
    • increment the number of months
  4.  Also within the loop we need to:
    • decrease the value of both old and new cars
    • increase the percent loss every two months
  5. Finally we need to return the number of months until we can afford the new car, and any change left over from the purchase

Here we go (using Javascript):

  1. Set the variables to zero[pastacode lang=”javascript” manual=”function%20carBuying(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%7D” message=”” highlight=”” provider=”manual”/]
  2. Start the while loop by setting the condition and then within the loop add to the savings and increment the number of months[pastacode lang=”javascript” manual=”while%20(startPriceNew%20%3E%20startPriceOld%20%2B%20savings)%7B%0A%09%09savings%20%2B%3D%20savingperMonth%3B%0A%09%09numOfMonths%20%2B%3D%201%3B%0A%7D” message=”” highlight=”” provider=”manual”/]
  3. Decrease the value of both old and new cars by the percent loss per month amount[pastacode lang=”javascript” manual=”while%20(startPriceNew%20%3E%20startPriceOld%20%2B%20savings)%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%7D” message=”” highlight=”” provider=”manual”/]
  4. Increase the percent loss every two months. I found this part a bit tricky. The best way to implement a check if the number of months is even is by using the modulo (remainder) operator (%). However when I used:[pastacode lang=”javascript” manual=”if%20(numOfMonths%20%25%202%20%3D%3D%3D%200)%20%7B%0A%09%09%09percentLossByMonth%20%2B%3D%200.5%3B%0A%09%09%7D” message=”” highlight=”” provider=”manual”/]

    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”/]

  5. To finish up we need to return the number of months and the surplus amount (rounded to nearest integer) when taking cost of the new car from the savings plus the value of the old car[pastacode lang=”javascript” manual=”return%20%5BnumOfMonths%2C%20Math.round(startPriceOld%20%2B%20savings%20-%20startPriceNew)%5D%3B” 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!