|
-
Feb 2nd, 2013, 07:36 PM
#1
Thread Starter
Frenzied Member
Implement JS NumberFormat Function
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
HTML 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);
[COLOR="#FF0000"]totalpaymentamt= formatDollar(totalpayment);[/COLOR]
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>
-
Feb 3rd, 2013, 01:16 AM
#2
Thread Starter
Frenzied Member
Re: Implement JS NumberFormat Function
Found my problem, there was an error method 'reduce' not supported. I found this following work around code and thought I would share it
Code:
if (!Array.prototype.reduce) {
Array.prototype.reduce = function reduce(accumulator){
if (this===null || this===undefined) throw new TypeError("Object is null or undefined");
var i = 0, l = this.length >> 0, curr;
if(typeof accumulator !== "function") // ES5 : "If IsCallable(callbackfn) is false, throw a TypeError exception."
throw new TypeError("First argument is not callable");
if(arguments.length < 2) {
if (l === 0) throw new TypeError("Array length is 0 and no second argument");
curr = this[0];
i = 1; // start accumulating at the second element
}
else
curr = arguments[1];
while (i < l) {
if(i in this) curr = accumulator.call(undefined, curr, this[i], i, this);
++i;
}
return curr;
};
}
-
Feb 3rd, 2013, 08:31 AM
#3
Re: Implement JS NumberFormat Function
Take note if you're building a "calculator" as most calculator apps use special formats for storing numbers. As an example, open the JavaScript console in your favourite browser and execute 0.1+0.2. The answer you get won't be 0.3, which is what you would expect it to be.
http://dev.opera.com/articles/view/w...real-decimals/
http://tinyurl.com/cemen4z
http://stackoverflow.com/questions/5...nt-math-broken
http://docs.oracle.com/cd/E19957-01/..._goldberg.html
In short, maybe a calculator isn't the best thing to be implementing for a first learning project. On the other hand, it will teach some of the more exotic stuff that JavaScript can trip you up on.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|