Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

a project that i came up with.

this a project of my own and i was hoping you guys can tell me any suggestions on how to improve it. my main focus is JavaScript at the moment. but here all the code. Any help would be appreciate thank you!!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
   <h1>daily and monthly interest</h1>
   <div class="container">
        <input type="number" id="amount" placeholder="total amount">
        <input type="number" id="interest" placeholder="interest">
        <input type="number" id="days-in-year" placeholder="days in year">
        <input type="number" id="last-payment" placeholder="days since last payment">
        <button id="calculate">calculate</button>
   </div>
   <h4 id="daily-interest">here</h4>
   <h4 id="monthly-interest">here</h4>
    <script src="app.js"></script>
</body>
</html>

here is the css

body {
    min-height: 100vh;
    background: #262626;
}

input {
    width: 50vh;
    padding: 3px 0 3px 3px;
    outline: none;
    border: none;
    background: #e6e6e6;
    text-align: center;
    border: 1px solid #d9d9d9;
    display: block;
    margin: 10px auto;
    font-size: 1.3rem;
    text-transform: capitalize;
}

h1,
h4 {
    text-transform: capitalize;
    text-align: center;
    color: #00802b;
    font-family: Arial, Helvetica, sans-serif;
    display: none;
}

h4 {
    font-size: 1.5rem;
}
.container {
    padding: 10px 0;
    max-width: 90%;
    margin: auto;
    background: #333333;
}

button {
   display: block;
   width: 25vh;
   margin: 0 auto 10px;
   cursor: pointer;
   font-size: 1.3rem;

}

input:hover {
    background: #d9d9d9;
}
/* JavaScript class */
.alert {
    color: firebrick;
}

here is the JavaScript

const amount = document.getElementById('amount');
const interest = document.getElementById('interest');
const daysInYear = document.getElementById('days-in-year');
const lastPaymentDay = document.getElementById('last-payment');

const dailyInterest = document.getElementById('daily-interest');
const monthlyInterest = document.getElementById('monthly-interest');

const calculate = document.getElementById('calculate');

calculate.addEventListener('click', () => {
    let amountVa = amount.value;
    let interestVa = interest.value;
    let daysInYearVa = daysInYear.value;
    let lastPaymentDayVa = lastPaymentDay.value;

    let totalDailyApr = (amountVa / 100) * interestVa / daysInYearVa;
    let totalMonthlyApr = (amountVa / 100) * interestVa / daysInYearVa * lastPaymentDayVa;

    dailyAprEmpty(amountVa, dailyInterest, totalDailyApr);
    monthlyAprEmpy(amountVa, monthlyInterest, totalMonthlyApr);
});

// daily interest function
function dailyAprEmpty(input, text, value) {
    if (input === '') {
        text.textContent = 'please enter a number';
        text.classList.add('alert');
        text.style.display = 'block';
        setTimeout(() => {
            text.classList.remove('alert');
            text.style.display = 'none';
        }, 2000);
    } else {
        text.style.display = 'block';
        text.textContent = `${value}  is your daily APR`;
        setTimeout(() => {
            text.style.display = 'none'
        }, 6000);
    }
}

// monthly interest function
function monthlyAprEmpy(input, monthly, value) {
    if (input === '') {
        monthly.style.display = 'none';
    } else {
        monthly.style.display = 'block';
        monthly.textContent = `${value} is your monthly APR`;
        setTimeout(() => {
            monthly.style.display = 'none'
        }, 6000);
    }
}

one thing i couldnt figured out was how to get the percentage value to only give me the first two decimal numbers like 2.50 instead i get 2.504213545.

1 Answer

To display only two decimal places you can use the toFixed() method:

 value = value.toFixed(2)

where 2 is the number of decimal places

thank you so much for taking the time. and yes now it works how i wanted.