Example on how to do a rolling daily budget

Shows how to calculate spend on a daily rollin budget

by patrickml

HTML

<link rel="stylesheet" href="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css">
<script src="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
<script src="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>
<title>Jasmine Spec Runner</title>

JavaScript

//http://opensas.wordpress.com/2013/06/23/sharing-you-javascript-ninja-secrets-run-your-jasmine-tests-on-jsfiddle/

const daily = (budget, fixed, spent, current_day, days_in_month) => {
    const net_budget = budget - fixed;
    const daily_budget = net_budget / days_in_month;
    const rollover = ((daily_budget * (current_day - 1)) - spent);
 
    return rollover + daily_budget;
 }

const monthly_budget = 6500;
const fxd = 4000;
const dim = 31;

const examples = [
	[20, 60],
  [5, 136],
  [0, 216],
  [0, 297],
  [170, 208],
  [20, 268],
  [200, 149],
  [300, -70],
  [0, 10],
  [0, 91],
  [0, 172],
  [50, 202]
];

describe("Daily Roll over Budget", function(){
    examples.forEach(([spend, budget], idx) => {
      const spent_to_day = examples.slice(0, idx + 1).reduce((m, c) => m + c[0], 0);
    	it(`Should alow me to spend $${budget} on day ${idx + 1} if my spend to date is $${spent_to_day}`, function() {
				const daily_budget = daily(monthly_budget, fxd, spent_to_day, idx + 1, dim);
      	expect(Math.floor(daily_budget)).toEqual(budget);
    	});
    });
});

// load jasmine htmlReporter
(function() {
  var env = jasmine.getEnv();
  env.addReporter(new jasmine.HtmlReporter());
  env.execute();
}());