開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- jQuery (Library)
- Safari(Web browser)
Exercises for Programmers: 57 Challenges to Develop Your Coding Skills (Brian P. Hogan 著、Pragmatic Bookshelf)のChapter 3(Calculations)、12(Computing Simple Interest)を取り組んでみる。
12(Computing Simple Interest)
コード(Emacs)
<label for="principal">
Etner the principal:
</label>
<input id="principal" type="number" min="0" step=".01" value="1500">
<br>
<label for="rate">
Enter the rate of interest:
</label>
<input id="rate" type="number" min="0" step="0.01" value="4.3">
<br>
<label for="years">
Enter the number of years:
</label>
<input id="years" type="number" min="0" step="1" value="4">
<br>
<p>
After <span class="years"></span> years at <span class="rate"></span>% the
investment will be worth $<span class="amount"></span>.
</p>
<script src="sample12.js"></script>
(function () {
'use strict';
var input_principal = document.querySelector('#principal'),
input_rate = document.querySelector('#rate'),
input_years = document.querySelector('#years'),
inputs = [input_principal, input_rate, input_years],
span_years = document.querySelectorAll('.years'),
span_rate = document.querySelectorAll('.rate'),
span_amount = document.querySelectorAll('.amount'),
calc,
display;
calc = function (principal, rate, years) {
return Math.round((principal * (1 + rate * years)) * 100) / 100;
};
display = function () {
var amount,
principal = parseFloat(input_principal.value),
rate = parseFloat(input_rate.value),
years = parseInt(input_years.value, 10),
i,
max;
amount = calc(principal, rate / 100, years);
for (i = 0, max = span_years.length; i < max; i += 1) {
span_years[i].innerText = years;
}
for (i = 0, max = span_rate.length; i < max; i += 1) {
span_rate[i].innerText = rate;
}
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();
}());
After years at % the investment will be worth $.
0 コメント:
コメントを投稿