Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SpencerCooley/2401214 to your computer and use it in GitHub Desktop.
Save SpencerCooley/2401214 to your computer and use it in GitHub Desktop.
//calculates the new bill based on a number and a percent savings that you feed it.
function newBill(bill, percentSavings){
var result = -1*(bill*percentSavings)+bill;
return result;
}
function finalCalculationWithCheckedProducts(array, originalBill){
var stopCase = array.length -1;
var i=0;
var averageMonthlyBill = originalBill;
for (i=0;i<=stopCase;i++)
{
var iterateBill = newBill(averageMonthlyBill,array[i]);
averageMonthlyBill = iterateBill;
}
//ends the for loop
return averageMonthlyBill;
}
//tests for this code
//final calculation tests
describe("calculation for savings", function(){
it("in the case that we have a $300 bill with a 20% savings the new bill (after savings) should be 240", function(){
var bill = 300;
var percentSavings = 0.2;
expect(newBill(bill, percentSavings)).toEqual(240);
});
});
//final calculation tests
describe("Final calculation", function(){
it("with a average monthly bill of $300 and purchase of products that save 20%, 5%, and 10% anually; the new monthly average should be 205.2", function(){
var percentages = [0.2,0.05,0.10];
var bill = 300;
expect(finalCalculationWithCheckedProducts(percentages,bill)).toEqual(205.2);
});
});
//final calculation tests
describe("The order of percentages in array should not matter.", function(){
it("with a average monthly bill of $300 and purchase of products that save 5%, 20%,and 10% anually; the new monthly average should be 205.2", function(){
var percentages = [0.05,0.2,0.10];
var bill = 300;
expect(finalCalculationWithCheckedProducts(percentages,bill)).toEqual(205.2);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment