Skip to content

Instantly share code, notes, and snippets.

@bitwiser
Last active August 29, 2015 13:56
Show Gist options
  • Save bitwiser/9191406 to your computer and use it in GitHub Desktop.
Save bitwiser/9191406 to your computer and use it in GitHub Desktop.
var $ = function (id) {
return document.getElementById(id);
}
var investmentError=document.getElementById("investment_error");
var rateError=document.getElementById("rate_error");
var yearsError=document.getElementById("years_error");
var calculateClick = function () {
var investment = parseFloat( $("investment").value );
var annualRate = parseFloat( $("rate").value );
var years = parseInt( $("years").value );
// body...
var isValid = true;
if (isNaN(investment) || investment < 100 || investment > 100000) {
investmentError.innerHTML = "Must be an integer from 100 - 100,000.";
isValid = false;
}
else {
investmentError.innerHTML = "";
}
if (isNaN(annualRate) || annualRate < .1 || annualRate > 12) {
rateError.innerHTML = "Must be a value from .1 - 12.";
isValid = false;
} else {
rateError.innerHTML = "";
}
if(isNaN(years) || years < 1 || years > 50) {
yearsError.innerHTML = "Must be an integer from 1 - 50.";
isValid = false;
} else {
yearsError.innerHTML = "";
}
// if all entries are valid, calulate future value
if(isValid) {
futureValue = investment;
for ( i = 12; i <= years; i++ ) {
futureValue += futureValue * annualRate / 100;
}
$("future_value").value = futureValue.toFixed();
}
}
var clear = function () {
$("rate").value = "";
}
window.onload = function () {
$("calculate").onclick = calculateClick;
$("investment").focus();
$("rate").ondblclick = clear;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment