Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
    <form class="mb-2 form" data-time="bank-form">
        <div class="form__field flex mb-1">
            <label for="total-amount" data-time="input-total-amount">Enter amount</label>
            <input class="input" type="text" id="total-amount" name="total-amount" placeholder="e.g. 10000 or 10,000" data-time="total-amount">
        </div>
        <div class="form__field flex mb-1">
            <label for="interest-rate">Enter interest Rate (Do not include % sign)</label>
            <input type="text" id="interest-rate" name="interest-rate" data-time="interest" placeholder="e.g. 10">
        </div>
        <div class="form__field flex mb-1">
            <label for="year">Enter Year</label>
            <input type="text" id="year" name="year" data-time="year" placeholder="e.g. 5">
        </div>
        <div class="form__field flex mb-1">
            <label for="compound-frequency">Enter Compound frequency</label>
            <select id="compound-frequency" data-time="compound-frequency" name="compound-frequency">
                <option value="1" selected>Annually / Yearly</option>
                <option value="2">Semi Annually / Half yearly</option>
                <option value="4">Quarterly</option>
                <option value="6">Bimonthly</option>
                <option value="12">Monthly</option>
                <option value="52">Weekly</option>
                <option value="365">Daily</option>
            </select>
        </div>
        <input class="btn" type="submit" value="Submit">
    </form>
    <article class="mb-2" data-time="render"></article>
</div>
              
            
!

CSS

              
                :root {
    --spacer: 1rem;
}

*,
*::after,
*::before {
    box-sizing: border-box;
}

body {
    line-height: 1.5;
    font-size: 1.125rem;
}

h1,
h2,
h3 {
    line-height: 1;
}

.container {
    width: 90%;
    max-width: 1600px;
    margin-inline: auto;
}

// .flex {
//     display: flex;
//     flex-direction: column;
// }

.mb-0 {
    margin-bottom: calc(var(--spacer) * 0);
}

.mb-2 {
    margin-bottom: calc(var(--spacer) * 2);
}

.pl-4 {
    padding-left: calc(var(--spacer) * 4);
}

// form
.form-control {
    display: block;
    width: 100%;
    padding: 0.375rem 0.75rem;
    font-size: 1rem;
    font-weight: 400;
    line-height: 1.5;
    background-color: var(--white);
    background-clip: padding-box;
    border: 1px solid var(--gray-light);
    appearance: none;
    border-radius: 0.25rem;
    transition: border-color 0.15s ease-in-out;
}

.form {
    display: block;

    &__field {
        margin-top: 1rem;
        margin-bottom: 1rem;

        & > label {
            display: block;
            margin-bottom: 0.3125rem;
        }

        & > input,
        & > select {
            width: 100%;
            padding: 0.625rem;
            border-radius: 0.5rem;
            border: 2px solid #e5e7eb;
            transition: all 0.3s ease-in-out;
            background-color: #fdfdfd;
        }

        & > input:focus,
        select:focus {
            outline: none;
            border: 2px solid #5aa5be;
        }
    }
}

// button
.btn {
    background-color: hsl(204, 88%, 53%);
    border: none;
    border-radius: 0.25rem;
    color: var(--white);
    cursor: pointer;
    display: inline-block;
    text-align: center;
    text-decoration: none;
    vertical-align: middle;
    padding: 0.375rem 0.75rem;
    transition: background-color 0.15s ease-in-out;

    &:hover {
        background-color: hsl(204, 88%, 40%);
    }
}

              
            
!

JS

              
                class BankCalculation {
    amount: string;
    year: number;
    interest: number;
    compound: number;

    constructor(
        amount: string,
        year: number,
        interest: number,
        compound: number
    ) {
        this.amount = amount;
        this.year = year;
        this.interest = interest / 100;
        this.compound = compound;
    }

    myFutureAmount(renderArea: HTMLElement): void {
        const totalInterest: number = this.getCalcInterestWithPower();
        const answer = Math.round(
            Number(this.amount.replaceAll(",", "")) * totalInterest
        );
        this.renderTemplate(`$${answer.toLocaleString("en-US")}`, renderArea);
    }

    protected getCalcInterestWithPower(): number {
        let year: number = this.year;
        const interest: number = this.getCalcInterest();
        let calcPower: number = interest ** year;
        if (this.isCompound()) {
            year = year * this.compound;
            calcPower = interest ** year;
        }
        return Number(calcPower.toFixed(5));
    }

    protected getCalcInterest(): number {
        let interest: number = 1 + this.interest;
        if (this.isCompound()) {
            interest = 1 + this.interest / this.compound;
        }
        return Number(interest.toFixed(5));
    }

    protected isCompound(): boolean {
        return this.compound > 1 ? true : false;
    }

    protected renderTemplate(answer: string, renderArea: HTMLElement) {
        renderArea.innerHTML = `<div>Your ${this.amount} after ${this.year} year will be ${answer}</div>`;
    }
}

const FORM = document.querySelector(
    '[data-time="bank-form"]'
) as HTMLFormElement;
const DOM = {
    totalAmount: document.querySelector(
        '[data-time="total-amount"]'
    ) as HTMLInputElement,
    interest: document.querySelector(
        '[data-time="interest"]'
    ) as HTMLInputElement,
    year: document.querySelector('[data-time="year"]') as HTMLInputElement,
    compound: document.querySelector(
        '[data-time="compound-frequency"]'
    ) as HTMLInputElement,
    renderContainer: document.querySelector(
        '[data-time="render"]'
    ) as HTMLElement
};

FORM.addEventListener("submit", (event) => {
    event.preventDefault();
    const total_amount: string = DOM.totalAmount.value;
    const interest: number = Number(DOM.interest.value);
    const year: number = Number(DOM.year.value);
    const compound: number = Number(DOM.compound.value);

    const bankCalculation = new BankCalculation(
        total_amount,
        year,
        interest,
        compound
    );
    if (total_amount !== "" && interest !== 0 && year !== 0) {
        bankCalculation.myFutureAmount(DOM.renderContainer);
    } else {
        alert("Fill out all the input box.");
    }
});

              
            
!
999px

Console