Oppgave 8

Resultat

Koden

JavaScript

                
window.onload = ready;

function ready() {
    document.getElementById("o8-calculate").onclick = calculate;
}

function calculate() {
    var amount = parseInt(document.getElementById("o8-amount").value);
    var years = parseInt(document.getElementById("o8-years").value);
    var interest = parseFloat(document.getElementById("o8-interest").value);
    var output = document.getElementById("o8-output");

    output.value = "";

    for (var i = 0; i < years; i++) {
        amount += (amount*interest)/100;
        output.value += "Penger i banken etter " + (i+1) + " år: " + amount.toFixed(2) + "\n"; // Output med 2 des
    }
}
                
            

HTML

                
<div class="row input-field">
    <label for="o8-amount" class="col s1"><i class="icon-money"></i></label>
    <input type="number" id="o8-amount" class="col s11" placeholder="Pengebeløp">
</div>
<div class="row input-field">
    <label for="o8-years" class="col s1"><i class="icon-calendar-2"></i></label>
    <input type="number" id="o8-years" class="col s11" placeholder="Antall år">
</div>
<div class="row input-field">
    <label for="o8-interest" class="col s1"><i class="icon-percent-up"></i></label>
    <input type="number" id="o8-interest" class="col s11" placeholder="Rente">
</div>
<div class="row input-field center">
    <button id="o8-calculate" class="btn black">Regn ut</button>
</div>
<div class="row input-field">
    <textarea id="o8-output" rows="10" readonly></textarea>
</div>