2016年8月21日日曜日

開発環境

Exercises for Programmers: 57 Challenges to Develop Your Coding Skills (Brian P. Hogan 著、Pragmatic Bookshelf)のChapter 3(Calculations)、13(Determining Compound Interest)を取り組んでみる。

13(Determining Compound Interest)

コード(Emacs)

<label for="principal">
  What is the principal amount?
</label> 
<input id="principal" type="number" min="0" step="1" value="1500">
<br>
<label for="rate">
  What is the rate?
</label>
<input id="rate" type="number" min="0" step="0.01" value="4.3">
<br>
<label for="years">
  What is the number of years?
</label> 
<input id="years" type="number" min="0" step="1" value="6">
<br>
<label for="times">
  What is the number of times the interest is compounded per year? 
</label> 
<input id="times" type="number" min="0" step="1" value="4">
<br>
<p>
  $<span class="principal"></span> invested at <span class="rate"></span>% for
  <span class="years"></span> years compounded <span class="times"></span> times
  per year is $<span class="amount"></span>.
</p>

<script src="sample13.js"></script>
(function () {
    'use strict';
    var input_principal = document.querySelector('#principal'),
        input_rate = document.querySelector('#rate'),
        input_years = document.querySelector('#years'),
        input_times = document.querySelector('#times'),
        inputs = [input_principal, input_rate, input_years, input_times],

        span_principal = document.querySelectorAll('.principal'),
        span_rate = document.querySelectorAll('.rate'),
        span_years = document.querySelectorAll('.years'),
        span_times = document.querySelectorAll('.times'),    
        span_amount = document.querySelectorAll('.amount'),
        
        calc,
        display;

    calc = function (principal, rate, years, times) {
        return Math.round(
            principal * Math.pow(
                (1 + (rate / 100) / times), times * years
            ) * 100
        ) / 100;
    };

    display = function () {
        var amount,
            principal = parseFloat(input_principal.value),
            rate = parseFloat(input_rate.value),
            years = parseInt(input_years.value, 10),
            times = parseInt(input_times.value, 10),
            i,
            max;

        amount = calc(principal, rate, years, times);

        for (i = 0, max = span_principal.length; i < max; i += 1) {
            span_principal[i].innerText = principal;
        }
        for (i = 0, max = span_rate.length; i < max; i += 1) {
            span_rate[i].innerText = rate;
        }
        for (i = 0, max = span_years.length; i < max; i += 1) {
            span_years[i].innerText = years;
        }
        for (i = 0, max = span_times.length; i < max; i += 1) {
            span_times[i].innerText = times;
        }
        for (i = 0, max = span_amount.length; i < max; i += 1) {
            span_amount[i].innerText = amount;
        }
    };

    inputs.forEach(function (input) {
        input.onchange = display;
        input.onkeyup = display;
    });

    display();
}());




$ invested at % for years compounded times per year is $.

0 コメント:

コメントを投稿