Hi guys I am brand new to JS and building a small calculator project just to learn:
Calculator is working and I a now trying to implement a Format function I got by googling. I am obviously doing something wrong because all my attempts fail and in fact stop the rest of my JS from running
Here is what I have:
The line in red is my attempt at implementing the formatDollar( ) function
Can someone show me where I am going wrong
Thanks
Code:
<script type="text/javascript">
<!--
function formatDollar(value) {
var p = num.toFixed(2).split(".");
return "$" + p[0].split("").reverse().reduce(function(acc, num, i, orig) {
return num + (i && !(i % 3) ? "," : "") + acc;
}, "") + "." + p[1];
}
function centNotation(value)
{
dollars = Math.floor(value); // chop floating point portion
cents = Math.floor((value % 1) * 100); // and chop everything else
if (cents < 10) cents = "" + cents + "0";
return(dollars + "." + cents);
}
function validInt(value)
{
for (var i=0; i < value.length; i++) {
var c = value.charAt(i);
if ((c < '0' || c > '9') && c != '.') {
alert("There's a problem: all values herein must " +
"be numbers only, and " + value + " has an unacceptable character " +
"'" + c + "'");
return(false);
}
}
return(true);
}
function myPaymentCalc()
{
var interestRate = document.debtcalc.interest.value,
monthlyPayment = document.debtcalc.monthlypayment.value,
principal = document.debtcalc.principal.value,
accumulatingInterest = 1;
if (! validInt(interestRate) || ! validInt(monthlyPayment) ||
! validInt(principal))
return(false);
if (interestRate == 0 || monthlyPayment == 0 || principal == 0) {
alert("You need to specify your monthly payment, credit card " +
"interest rate and current debt amount before I can calculate " +
"how long it'll take for you to pay off your debt.");
return(false);
}
interestRate = (interestRate > 1)? interestRate / 100.0 : interestRate;
monthlyInterestRate = interestRate / 12;
if (principal * monthlyInterestRate > monthlyPayment) {
monthlyrate = "$" + centNotation(principal * monthlyInterestRate);
permonthPayment = "$" + centNotation(monthlyPayment);
alert("You've got a fundamental problem: your monthly payment of " +
permonthPayment + " is less than the interest added each month " +
"(which is " + monthlyrate + ") and you will never " +
"pay off this debt. It might be a good time to call your creditors.");
return(false);
}
interimVal1 = monthlyPayment - (monthlyInterestRate * principal)
numberOfPayments = (Math.log(monthlyPayment) - Math.log(interimVal1)) /
Math.log(1+monthlyInterestRate);
numberOfPayments = Math.ceil(numberOfPayments);
interimVal2 = Math.pow((monthlyInterestRate+1), (numberOfPayments-1));
remaining = (principal * interimVal2) -
(monthlyPayment/monthlyInterestRate) * (interimVal2 - 1);
totalPayment = monthlyPayment *(numberOfPayments-1) + remaining;
payments = numberOfPayments;
totalpayment = "$" + centNotation(totalPayment);
totalpaymentamt= formatDollar(totalpayment);
interestPaid = "$" + centNotation(totalPayment-principal);
alert("Given these figures, you'll end up having " + payments +
" payments and pay a total of " + totalpayment + ", of which " +
interestPaid + " is interest.");
document.debtcalc.numberofpayments.value = payments;
document.debtcalc.totalpayments.value = totalpayment;
document.debtcalc.intpmt.value = interestPaid;
}
// -->
</script>