Code coverage report for financial/payment.js

Statements: 100% (14 / 14)      Branches: 100% (12 / 12)      Functions: 100% (3 / 3)      Lines: 100% (13 / 13)     

All files » financial/ » payment.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 291               1 18 18 1 17 2   15 15 15   10   15       1      
define(['./compoundInterest'], function (compoundInterest) {
 
    /**
     * Calculates the payment for a loan based on constant payments and
     * a constant interest rate.
     * http://en.wikipedia.org/wiki/Annuity_%28finance_theory%29
     * @version 0.1.1 (2012/03/20)
     */
    function payment(rate, nPeriods, presentValue, futureValue, isDue){
        futureValue = futureValue || 0;
        if (!presentValue && !futureValue) {
            return 0;
        } else if (rate === 0) {
            return (presentValue + futureValue) / nPeriods;
        } else {
            var r;
            r = (rate / (1 - Math.pow(1 + rate, -nPeriods))) * presentValue;
            if (futureValue) {
                //the "opposite" of financial/futureValue
                r += futureValue / ((Math.pow(1 + rate, nPeriods) -1) / rate);
            }
            return isDue? r / (1 + rate) : r;
        }
    }
 
    return payment;
 
});