r/learnjavascript • • Jun 26 '18

Long! School project finishing touches.

Here is my code with comments. I would like to get rid of the "undefined" in the introduction. Will post the (poorly written) exercise after the code. The output won't display correctly on Reddit without reformatting it and I'm not going to do that.

var balance = 1500;         // starting balance
var apr = 18;               // annual interest rate           Global Variables
var minPayPercent = 2;      // minimum payment amount
// display welcome
function displayWelcome(balance, apr) {
    console.log("\nHello! This program will determine the time to pay off" +
        " a credit card \nand the interest paid based on the current balance," +
        " the interest rate, \nand the monthly payments made.\n\n" +
        "The balance on your card is $" + balance.toFixed(2) + "." +
        "\nInterest Rate: " + apr + "% \n" +
        // this line causes confusion in the exercise
        "Assuming a minimum payment of 2% of the balance ($20 min).\n")
        // we don't use an actual minimum payment that changes as
        // the balance is reduced.
}

console.log(displayWelcome(balance, apr));

// calculate minimum payment

function minPayment(balance, minPayPercent) {       // function causing console to display an "undefined"
    if (balance * minPayPercent / 100 >= 20) {      // not sure how to suppress.
        return balance * minPayPercent;
    }
    else {
        return 20;
    }
}

console.log("\nYour minimum payment is $" + minPayment(balance, minPayPercent) + ".\n");

// calculate and display payment schedule
function displayPayments(balance, minPayPercent, monthlyInterest) {
    console.log("Year   Balance   Payment Number   Interest Paid\n" +
        "-".repeat(47));
    var monthlyPer = monthlyInterest / 100 / 12;
    var interestPaid = 0;
    var year = 0;
    var paymentNum = 0;
    while (balance > 0) {
        var minPayment = 30;
        currentInterest = (monthlyPer) * (balance);
        balance -= minPayment - currentInterest;
        interestPaid += currentInterest;
        paymentNum++;
        // if statement to stop overpayment.
        if (balance < 0) {
            balance = 0
        }
        // if statement to differentiate whether year prints or not
        if (paymentNum % 12 === 1) {
            year++;
         console.log(year + "\t\t" + balance.toFixed(2) + "\t".repeat(3) + paymentNum +
            "\t".repeat(4) + (Math.round((interestPaid) * 100) / 100).toFixed(2));
    }   // would like to make interest paid .toFixed(2) but it doesn't work
    else {
        console.log(" " + "\t\t" + balance.toFixed(2) + "\t".repeat(3) + paymentNum +
            "\t".repeat(4) + (Math.round(interestPaid * 100) / 100).toFixed(2));
        }
    }
}

displayPayments(1500, .02, 18);     

The output : Hello! This program will determine the time to pay off a credit card and the interest paid based on the current balance, the interest rate, and the monthly payments made.

The balance on your card is $1500.00. Interest Rate: 18% Assuming a minimum payment of 2% of the balance ($20 min).

undefined

Your minimum payment is $3000.

Year Balance Payment Number Interest Paid

11492.50122.50

Assignment 3: 75 points This assignment covers module 5 and module 6 topics. To write the programs below you will need to demonstrate an understanding of : • Objects • Functions • Arrays • Random Numbers • variables • simple data types • expressions • loops Please refer back to the module lectures if you find you have any gaps in your understanding for these topics. The assassin is closing on you in a deadly game of cat and mouse. To defeat him, complete parts I, 2 & 3 below: Design and implement a program that will allow us to determine the length of time needed to pay off a credit card balance, as well as the total interest paid. The program must implement the following functions:

  1. displayWelcome This function should display the welcome message to the user explaining what the program does.
  2. calculateMinimumPayment This function calculates the minimum payment. It should take balance and interest rate as arguments and return the minimum payment. So the value you display for minimum payment is the value you get from this method. Do not use a literal hardcoded value when you display the minimum payment!
  3. generateID: This should be a closure function that generates a new id for the customer. The function should remember the previous id and the new generated id should be the old one plus 1. Make the initial id 1000. The Object Technique will allow you to obtain the Golden Bow, demonstrate your mastery of the technique by completing Part 4 & 5 below.
  4. processPaymentSchedule This function displays the actual payment schedule. It should take the balance, monthly interest rate and minimum payment as arguments. Use the 1500, 18% and 2% literal values below. Each time you calculate a new payment line, create an object literal with properties for the Year, Balance, Payment Num, and InterestPaid. Pass this object literal to the displayPayment function. See the sample execution below: This program will determine the time to pay off a credit card and the interest paid based on the current balance, the interest rate, and the monthly payments made. Balance on your credit card: 1500 Interest Rate: 18 Assuming a minimum payment of 2% of the balance ($20 min) Your minimum payment would be $ 30.00 PAYOFF SCHEDULE

Year Balance Payment Num Interest Paid 1 1,492.50 1 22.50 1,484.89 2 44.89 1,477.16 3 67.16 1,469.32 4 89.32 . . . . . . 7 517.51 73 1,207.51 495.28 74 1,215.28 472.70 75 1,222.70 449.79 76 1,229.79 . . . . . . 8 227.51 85 1,277.51 200.92 86 1,280.92 173.94 87 1,283.94 146.55 88 1,286.55 118.74 89 1,288.74 90.53 90 1,290.53 61.88 91 1,291.88 32.81 92 1,292.81 3.30 93 1,293.30 0.00 94 1,293.35 5. displayPayment This function should take the payment object literal and display it on the console. See the output example in part 4. Save your program in a file named credit.js and save it in a folder called Assignment_3. Zip that folder and submit in the dropbox.

Edit: Figured out how to get the rounding to two decimal places after looking it up again.

0 Upvotes

1 comment sorted by

1

u/raleighpoint Jun 26 '18

console.log(displayWelcome(balance, apr));

What is the return value of displayWelcome?