 ## Theory In the face of massive problems like, “I need to pay back \$30,000 of this debt” or “I have this massive project above my head at work”, it’s very easy to get overwhelmed. Most of us will do what makes the most sense, nothing at all. And that’s okay! But most of the time, this leaves us feeling depressed and dejected. What can we do to solve these massive problems? This post will discuss using a method used in Computer Science called Divide and Conquer. This helps us get the solutions we want.

The divide and conquer algorithm is pretty straightforward. All we’ll want to do is split our problem into smaller and smaller subproblems until we get to the easiest subproblems possible. Ideally, the smallest subproblems solve themselves. Divide and Conquer is like Thanksgiving dinner. Your plate is overflowing with food, and there’s no way you could eat all of it in one go! But if you take one bite at a time, you can move towards completion. Let’s look at the example above: “\$30,000 in debt”. This is a huge problem, and oft times, it’s even scary to think about! Let’s see how we can divide this problem into more manageable chunks.

## Example

• \$30,000 in student loan debt
• Monthly payments: ~\$300/month for 10 years
• Principal: ~\$220/month * Interest: ~\$80/month
• 3 different loans with 3 different APR%
• Find the loan with the highest inetrest rate
• Loan 1: \$10,000 at 8% (This loan is our highest priority)
•  Put our \$300 a month towards this loan
• Calculate payoff time
• (\$10,000 * 1.08%) / \$300 = 36 months to pay off entirely
• Write down each month on paper with how much you’ll pay
• Find a pencil and paper
• \$300 / 30 (~days in a month) = ~ \$10 / day
• Build a budget
• Write down expenses on paper that you know happen each month (like rent and other bills)
• Take a pencil and write down what you bought today
• Think about what you can live without tomorrow or find a cheaper alternative
• Loan 2: \$10,000 at 5%
• Loan 3: \$10,000 at 3%

As you can see above, we broke this problem into manageable chunks (the bold ones)! We prioritized our loans into categories, and split our monthly payments into daily payments of \$10. This does a few things for us.

1. The problems now feel achievable – \$10 is an amount of money that’s not terrifying to think about
2. Setting our mindset – We’re now problem solvers, no longer victim to huge problems
3. Inspires us to do more – we know that \$10 a day is what we need, imagine if we could up that to \$15 with budgeting! Snowball effect!

## Code

This code in JavaScript that symbolizes the divide and conquer theory. Just like above, it basically helps us find manageable ways to solve our problems. You can divide and conquer on paper, but doing it with code is kinda the point of this blog 😉. Also, when written with code, we get to keep old problems around and organize new ones. We also get to mix and match.

### Principles

The code today is pretty straightforward. We’ll be using the Object Oriented class model. JavaScript doesn’t actually have classes, but in ES6, classes we’re added as a syntax sugar.

Here’s the full example:

 class Problem { // a constructor is whats called after an object is made // here we used defaults to set our variables constructor (value = '', points = default_points, { solved = false, solution = '', problems = [] } = {}) { this.value = value; this.solved = solved; this.points = points; this.solution = solution; this.problems = problems; } // append a problem to the list add_problem (problem) { this.problems.push(problem); } // solve your problem! solve () { this.solved = true; } // is our problem solved? isSolved () { return this.solved; } } // list of our problems starting with the smallest first const small_problem_1 = new Problem('ez mode', 25, {solution: 'Put on my left shoe'}); const small_problem_2 = new Problem('also ez', 25, {solution: 'Take a deep breath in'}); const medium_problem_1 = new Problem('This is still too hard', 50, {problems: [small_problem_1, small_problem_2]}); const medium_problem_2 = new Problem('I agree, I\'d rather not be solved', 50, {problems: [small_problem_1, small_problem_2]}); const big_problem = new Problem('WE\'VE GOT A BIG PROBLEM', 100, {problems: [medium_problem_1, medium_problem_2]}); // solve our problems from the bottom up! small_problem_1.solve(); console.log(`Small problem 1 solved?: \${small_problem_1.isSolved()}`);

view raw
prblm.js
hosted with ❤ by GitHub

This could be extended out a lot, as this is just the basic structure. We could:

• Make a webpage to visually add and remove problems
• Return a list of all the unsolved problems we have, sorted by difficulty
• Add a reward system to our code, giving us positive incentives to complete tasks

I hope this inspired you to go out and look at your problems differently. Take that first step!