Skip to content

Instantly share code, notes, and snippets.

@conormcafee
Last active January 8, 2016 16:31
Show Gist options
  • Save conormcafee/4403e76748ff2984fbc4 to your computer and use it in GitHub Desktop.
Save conormcafee/4403e76748ff2984fbc4 to your computer and use it in GitHub Desktop.
Savings
<form id="myform">
<h1>Please enter data</h1>
<input id="salary" type="number" placeholder="Salary" />
<br />
<input id="bills" type="number" placeholder="Bills" />
<br />
<input id="savings" type="number" placeholder="Savings" />
<br />
<input type="button" value="Save/Show" id="subButton"/>
<br />
<input type="button" value="Clear" id="clear"/>
</form>
<div id="salary">
<p id="totalSpend"></p>
</div>
// Enter Details
function details() {
var enterPay = document.getElementById('pay').value;
var enterBills = document.getElementById("bills").value;
var enterSavings = document.getElementById("savings").value;
var pay = parseInt(enterPay);
var bills = parseInt(enterBills);
var savings = parseInt(enterSavings);
var totalBillsSavings = roundToTwo(bills + savings);
document.getElementById("totalBillsSavings").innerHTML = "£" + Math.round(totalBillsSavings);
var salaryMinusTotalBillsSavings = roundToTwo(pay - totalBillsSavings);
console.log("Your balance after bills and savings is deducted = £" + salaryMinusTotalBillsSavings);
// Dates
var enterDate1 = document.getElementById('date1').value;
var enterDate2 = document.getElementById('date2').value;
console.log("You got paid on " + enterDate1 + " , and you next get paid on " + enterDate2);
var date1 = new Date(enterDate1);
var time1 = date1.getTime();
var date2 = new Date(enterDate2);
var time2 = date2.getTime();
var fullDay = 86400000;
var milliSecondDif = time2 - time1;
var dif = Math.round(milliSecondDif/fullDay);
console.log(dif + " days before you get paid!");
var daily = roundToTwo(salaryMinusTotalBillsSavings / dif);
console.log("You can afford to spend £" + daily + " daily!");
var weekly = roundToTwo(daily * 7);
console.log("You can afford to spend £" + weekly + " each week!");
}
// Clear Inputs
function clear () {
document.getElementById('myform').reset();
}
// Round to Two Decimal Places
function roundToTwo(value) {
return +(Math.round(value + "e+2") + "e-2");
}
// Buttons
document.getElementById('subButton').addEventListener('click', details);
document.getElementById('clear').addEventListener('click', clear);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment