<commit_before>var app = app || {};

(function() {
	'use strict';

	// Debt Model
	// ----------

	// Our basic **Debt** model has 'title', 'type', 'principle', 'rate', 
	// 'order', 'repayment', and 'monthly' attributes.
	app.Debt = Backbone.Model.extend({

		// Default attributes for the debt. Ensure that each debt created 
		// has 'title', 'type', 'principle', 'rate', 'repayment', and 'monthly' keys.
		defaults: {
			title: 'Double Click to Change',
			type: 'credit',
			principal: 5000,
			rate: 6.8,
			repayment: 10,
			monthly: 0
		},

		// The model listens for changes on itself in order properly
		// update its 'monthly' attribute value when it is first added and
		// subsequently on 'type', 'principal', 'rate', or 'repayment' change.
		initialize: function() {
			this.on( 'add change:type change:principal change:rate change:repayment', this.updateMonthly, this );
		},

		// Helper function that will update the model's 'monthly' attribute
		// based on the current 'type' value. Use the 'silent: true' option
		// when setting so that another 'change' event is not triggered on the model.
		updateMonthly: function( eventName ) {
			var payment = 0.0;

			switch( this.attributes.type ) {
				case 'Credit Card':
					payment = this.creditCardMonthly();
					break;
				case 'Stafford Loan':
					payment=  this.staffordLoanMonthly();
					break;
				case 'Perkins Loan':
					payment = this.perkinsLoanMonthly();
					break;
				case 'Plus Loan':
					payment = this.plusLoanMonthly();
					break
				default:
					payment = this.calculateMonthly(); 
					break;
			}

			// If the monthly payment is greater than the principal, 
			// simply use the principal. This also covers the specific
			// minimum payment stipulations for the various loan programs
			// and credit cards because their adjustments result in a higher
			// payment amount.
			if ( payment > this.attributes.principal ) {
				this.set( { monthly: this.attributes.principal }, { silent: true } );
			} else {
				this.set( { monthly: payment }, { silent: true } );
			}
		},

		// Function that calculates monthly payment for Credit Card debts.
		creditCardMonthly: function() {
			// payment = (1% * principal) + (rate / 12) * principal
			var rate = this.attributes.rate / 100.0;
			var principal = this.attributes.principal;
			return (0.01 * principal) + (rate / 12) * principal;
		},

		// Calculate monthly payment for Stafford Loan based on minimum payment.
		staffordLoanMonthly: function() {
			var payment = this.calculateMonthly();
			return payment > 50.0 ? payment : 50.0;
		},

		// Calculate monthly payment for Perkins Loan based on minimum payment.
		perkinsLoanMonthly: function() {
			var payment = this.calculateMonthly();
			return payment > 40.0 ? payment : 40.0;
		},

		// Calculate monthly payment for Plus Loan based on minimum payment.
		plusLoanMonthly: function() {
			var payment = this.calculateMonthly();
			return payment > 50.0 ? payment : 50.0;
		},

		// Calculate monthly payment based on compounding interest formula.
		//  P = L*i / (1 - 1/(1 + i)^n)
		// 	L = prinicpal 	
		// 	i = APR / 12
		// 	n = term in months
		calculateMonthly: function() {
			var i = (this.attributes.rate/100.0)/12.0;
			var L = this.attributes.principal;
			var n = this.attributes.repayment;

			return L*i / (1 - 1/Math.pow(1+i, n));
		}, 

		// Calculate total interest accrued over lifetime of this debt.
		// I = cN - P
		// 	c = cost of payment
		//	N = number of payments
		// 	P = principal<commit_msg>Added function stub that fixes the repayment time based on monthly minimum requirements for debts.<commit_after>var app = app || {};

(function() {
	'use strict';

	// Debt Model
	// ----------

	// Our basic **Debt** model has 'title', 'type', 'principle', 'rate', 
	// 'order', 'repayment', and 'monthly' attributes.
	app.Debt = Backbone.Model.extend({

		// Default attributes for the debt. Ensure that each debt created 
		// has 'title', 'type', 'principle', 'rate', 'repayment', and 'monthly' keys.
		defaults: {
			title: 'Double Click to Change',
			type: 'credit',
			principal: 5000,
			rate: 6.8,
			repayment: 10,
			monthly: 0
		},

		// The model listens for changes on itself in order properly
		// update its 'monthly' attribute value when it is first added and
		// subsequently on 'type', 'principal', 'rate', or 'repayment' change.
		initialize: function() {
			this.on( 'add change:type change:principal change:rate change:repayment', this.updateMonthly, this );
		},

		// Helper function that will update the model's 'monthly' attribute
		// based on the current 'type' value. Use the 'silent: true' option
		// when setting so that another 'change' event is not triggered on the model.
		updateMonthly: function( eventName ) {
			var payment = 0.0;

			switch( this.attributes.type ) {
				case 'Credit Card':
					payment = this.creditCardMonthly();
					break;
				case 'Stafford Loan':
					payment=  this.staffordLoanMonthly();
					break;
				case 'Perkins Loan':
					payment = this.perkinsLoanMonthly();
					break;
				case 'Plus Loan':
					payment = this.plusLoanMonthly();
					break
				default:
					payment = this.calculateMonthly(); 
					break;
			}

			// If the monthly payment is greater than the principal, 
			// simply use the principal. This also covers the specific
			// minimum payment stipulations for the various loan programs
			// and credit cards because their adjustments result in a higher
			// payment amount.
			if ( payment > this.attributes.principal ) {
				this.set( { monthly: this.attributes.principal }, { silent: true } );
			} else {
				this.set( { monthly: payment }, { silent: true } );
			}
		},

		// Function that calculates monthly payment for Credit Card debts.
		creditCardMonthly: function() {
			// payment = (1% * principal) + (rate / 12) * principal
			var rate = this.attributes.rate / 100.0;
			var principal = this.attributes.principal;
			return (0.01 * principal) + (rate / 12) * principal;
		},

		// Calculate monthly payment for Stafford Loan based on minimum payment.
		staffordLoanMonthly: function() {
			var payment = this.calculateMonthly();
			
			// Check to see if payment is less than loan agreement.
			// If so, adjust it and update the repayment time.
			if ( payment < STAFFORD_LOAN_MIN ) {
				this.fixRepayment( STAFFORD_LOAN_MIN );
				return STAFFORD_LOAN_MIN;
			} else {
				return payment;
			}
		},

		// Calculate monthly payment for Perkins Loan based on minimum payment.
		perkinsLoanMonthly: function() {
			var payment = this.calculateMonthly();
			
			// Check to see if payment is less than loan agreement.
			// If so, adjust it and update the repayment time.
			if ( payment < PERKINS_LOAN_MIN ) {
				this.fixRepayment( PERKINS_LOAN_MIN );
				return PERKINS_LOAN_MIN;
			} else {
				return payment;
			}
		},

		// Calculate monthly payment for Plus Loan based on minimum payment.
		plusLoanMonthly: function() {
			var payment = this.calculateMonthly();
			
			// Check to see if payment is less than loan agreement.
			// If so, adjust it and update the repayment time.
			if ( payment < PLUS_LOAN_MIN ) {
				this.fixRepayment( PLUS_LOAN_MIN );
				return PLUS_LOAN_MIN;
			} else {
				return payment;
			}
		},

		// Update the repayment time for this debt to factor in a minimum 
		// monthly repayment requirement.
		fixRepayment: function( payment ) {
			// TODO: Implement
		},

		// Calculate monthly payment based on compounding interest formula.
		//  P = L*i / (1 - 1/(1 + i)^n)
		// 	L = prinicpal 	
		// 	i = APR / 12
		// 	n = term in months
		calculateMonthly: function() {
			var i = (this.attributes.rate/100.0)/12.0;
			var L = this.attributes.principal;
			var n = this.attributes.repayment;

			return L*i / (1 - 1/Math.pow(1+i, n));
		}, 

		// Calculate total interest accrued over lifetime of this debt.
		// I = cN - P
		// 	c = cost of payment
		//	N = number of payments
		// 	P = principal
{'fraction_non_alphanumeric': 0.1194123556002009, 'fraction_numerical': 0.011803114013058764, 'mean_word_length': 2.8000954198473282, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '106735', 'n_tokens_mistral': 2961, 'n_tokens_neox': 2417, 'n_words': 843}