 |
Savings Goal
function valNumericStr(strFieldName, strValue, intBlankFlag, intMsgFlag){
// intBlankFlag == 0: can't be blank, intBlankFlag == !0: can be blank
// intMsgFlag == 0: no error messages, intMsgFlag == 1: error messages
if (strValue.length < 1){
if (intBlankFlag == 0){
if (intMsgFlag == 1)
alert("Please enter a value for " + strFieldName + ".");
return false;
}
}else{
var decPtAt = 0;
for (var i = 0; i < strValue.length; i++){
var localChar = strValue.charAt(i);
if (localChar < "0" || localChar > "9"){
if (localChar == "." && decPtAt == 0){
decPtAt = i + 1;
}else{
if (intMsgFlag == 1)
alert("Please remove the '" + localChar + "' character from " + strFieldName + ".");
return false;
}
}
}
}
return true;
}
function calcDeposit(){
var curAmount, curSavedNow, intYears, pctARate, pctPRate;
var intTermMonths, temp1, temp2, temp3;
curSavedNow = document.calculator.txtSavings.value;
if (valNumericStr("savings", curSavedNow, 0, 1) == false) curSavedNow = 0.00;
else curSavedNow = parseFloat(curSavedNow);
pctARate = document.calculator.txtAnnualRate.value;
if (valNumericStr("interest rate", pctARate, 0, 1) == false) pctARate = 0.00;
else pctARate = parseFloat(pctARate);
curAmount = document.calculator.txtAmount.value;
if (valNumericStr("total amount", curAmount, 0, 1) == false) curAmount = 0.00;
else curAmount = parseFloat(curAmount);
intYears = document.calculator.txtYears.value;
if (valNumericStr("years available to save", intYears, 0, 1) == false) intYears = 2;
else intYears = parseInt(intYears, 10);
// calculate
intTermMonths = parseInt(intYears, 10) * 12;
pctPRate = periodizeRate(pctARate, 12, 1);
temp1 = parseFloat(pctPRate) + 1;
temp2 = Math.pow(temp1, intTermMonths);
curSavedNow *= temp2;
curAmount -= curSavedNow;
if ( (parseFloat(temp2) - 1) > 0){
temp3 = (curAmount * pctPRate)/ (temp2 - 1);
}
else temp3 = 0;
if (temp3 < 0) temp3 = 0.00;
return formatFloat(temp3, 2);
}
function periodizeRate(annualRate, compoundPerYear, decimalFlag){
if (decimalFlag == 1) periodicRate = annualRate/(compoundPerYear * 100);
else periodicRate = annualRate/compoundPerYear;
return periodicRate;
}
function formatFloat (expr, decplaces){
// Based on Danny Goodman's JavaScript Bible (3rd ed) pg. 569
var str = "" + Math.round( eval(expr) * Math.pow(10, decplaces) );
while (str.length <= decplaces){
str = "0" + str;
}
var decpoint = str.length - decplaces;
return str.substring(0, decpoint) + "." + str.substring(decpoint, str.length);
}
There's always a good reason to save money, whether you are trying to buy a car, build a nest egg or for a travel!
Enter your savings goal below, and calculate the amount you will need to deposit each month to get to your goal!
................................................
|
|
 |