<commit_before>// Feature request:
// User provides different payments and calculator says how much faster you'll pay off the loan, and how much you'll save in interest while doing so.

$(function() {
    $('body').keyup(function(e) {
        // Update output when Enter key is pressed
        if (e.keyCode === 13) {
            updateOutput();
        }
    });
    $('input').keyup(function(e) { updateOutput(); });

    function updateOutput() {
        var loans = $('.loan');
        var numLoans = loans.length;
        var validLoans = [];

        for (var i = 0; i < numLoans; i++) {
            // Check if each row is valid. If 2+ rows are valid, then show output.
            // If all rows are valid, add a row.
            var principal = $('#principal' + (i + 1) + '-input').val();
            var interest = $('#rate' + (i + 1) + '-input').val();

            var loan = getValidRow(principal, interest);
            if (loan !== null) {
                loan.displayOrder = i;
                validLoans.push(loan);
            }
        }

        if (validLoans.length < 2) {
            return;
        }

        // Clear table before adding more rows
        var tableBody = $('#loan-output-table > tbody');
        tableBody.empty();

        // Show output
        // Calculate monthly interest and output each loan with this data, sorted by descending interest generated per loan.
        // In this table, also list the current amount that your "extra" payment should go to (if the "Desired Payment Beyond Minimum" is filled out. If not, say "Need 'Desired Payment Beyond Minimum' to calculate").
        validLoans.sort(function(a, b) {
            if (a.monthlyInterest < b.monthlyInterest) {
                return 1;
            }

            if (a.monthlyInterest > b.monthlyInterest) {
                return -1;
            }

            return 0;
        });

        var desiredPayment = getValidDecimal($('#payment-input').val());

        var distributions = calculatePayment(validLoans, desiredPayment);

        // Map payment distributions to loans
        for (var i = 0; i < distributions.length; i++) {
            validLoans[i].distribution = +distributions[i].toFixed(2);
        }

        // Restore original order for display
        validLoans.sort(function(a, b) {
            if (a.displayOrder < b.displayOrder) {
                return -1;
            }

            if (a.displayOrder > b.displayOrder) {
                return 1;
            }

            return 0;
        });

        for (var i = 0; i < validLoans.length; i++) {
            var distribution = 'Need "Desired Payment Beyond Minimum" to calculate.';

            var newRowHtml =    '<tr>' +
                                    '<td>' + (i + 1) + '</td>' +
                                    '<td>$' + validLoans[i].monthlyInterest + '</td>' +
                                    '<td>$' + validLoans[i].distribution + '</td>' +
                                '</tr>';

            tableBody.append(newRowHtml);
        }

        if (validLoans.length === numLoans) {
            addLoanRow();
        }
    }

    function calculatePayment(loans, remainder) {
        if (loans.length < 1) {
            console.log('Ran out of loans. Hopefully this is what normally happens.');
            return [0];
        }<commit_msg>Save loans in localStorage.<commit_after>// Feature request:
// User provides different payments and calculator says how much faster you'll pay off the loan, and how much you'll save in interest while doing so.

$(function() {
    $(document).ready(function() {
        loadLoans();
    });

    $('body').keyup(function(e) {
        // Update output when Enter key is pressed
        if (e.keyCode === 13) {
            updateOutput();
        }
    });

    $('input').keyup(function(e) { updateOutput(); });

    function loadLoans() {
        var json = localStorage.getItem('loans');
        if (!json) {
            return;
        }

        var loans = JSON.parse(json);
        console.log(loans);

        // TODO: Put loans in DOM
    }

    function saveLoans(loans) {
        localStorage.setItem('loans', JSON.stringify(loans));
    }

    function updateOutput() {
        var loans = $('.loan');
        var numLoans = loans.length;
        var validLoans = [];

        for (var i = 0; i < numLoans; i++) {
            // Check if each row is valid. If 2+ rows are valid, then show output.
            // If all rows are valid, add a row.
            var principal = $('#principal' + (i + 1) + '-input').val();
            var interest = $('#rate' + (i + 1) + '-input').val();

            var loan = getValidRow(principal, interest);
            if (loan !== null) {
                loan.displayOrder = i;
                validLoans.push(loan);
            }
        }

        if (validLoans.length < 2) {
            return;
        }

        // Clear table before adding more rows
        var tableBody = $('#loan-output-table > tbody');
        tableBody.empty();

        // Show output
        // Calculate monthly interest and output each loan with this data, sorted by descending interest generated per loan.
        // In this table, also list the current amount that your "extra" payment should go to (if the "Desired Payment Beyond Minimum" is filled out. If not, say "Need 'Desired Payment Beyond Minimum' to calculate").
        validLoans.sort(function(a, b) {
            if (a.monthlyInterest < b.monthlyInterest) {
                return 1;
            }

            if (a.monthlyInterest > b.monthlyInterest) {
                return -1;
            }

            return 0;
        });

        var desiredPayment = getValidDecimal($('#payment-input').val());

        var distributions = calculatePayment(validLoans, desiredPayment);

        // Map payment distributions to loans
        for (var i = 0; i < distributions.length; i++) {
            validLoans[i].distribution = +distributions[i].toFixed(2);
        }

        // Restore original order for display
        validLoans.sort(function(a, b) {
            if (a.displayOrder < b.displayOrder) {
                return -1;
            }

            if (a.displayOrder > b.displayOrder) {
                return 1;
            }

            return 0;
        });

        console.log(validLoans);

        for (var i = 0; i < validLoans.length; i++) {
            var distribution = 'Need "Desired Payment Beyond Minimum" to calculate.';

            var newRowHtml =    '<tr>' +
                                    '<td>' + (i + 1) + '</td>' +
                                    '<td>$' + validLoans[i].monthlyInterest + '</td>' +
                                    '<td>$' + validLoans[i].distribution + '</td>' +
                                '</tr>';

            tableBody.append(newRowHtml);
        }

        // Side effect
        saveLoans(validLoans);

        if (validLoans.length === numLoans) {
            addLoanRow();
        }
    }

    function calculatePayment(loans, remainder) {
        if (loans.length < 1) {
            console.log('Ran out of loans. Hopefully this is what normally happens.');
            return [0];
        }
{'fraction_non_alphanumeric': 0.12373562422059027, 'fraction_numerical': 0.0052653457115144795, 'mean_word_length': 1.5954692556634305, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '221965', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1885, 'n_words': 588}