開発環境
- 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)、11(Currency Conversion)を取り組んでみる。
11(Currency Conversion)
コード(Emacs)
<label for="amount_from">
How many euros are you exchanging?
</label>
<input id="amount_from" type="number" min="1" step="1" value="81">
<br>
<label for="rate_from">
What is the exchange rate?
</label>
<input id="rate_from" type="number" min="0" step="0.01" value="137.51">
<br>
<span class="amount_from"></span> euros at an exchange rate of
<span class="rate_from"></span> is <span class="amount_to"></span> U.S.
dollars.
<script src="sample11.js"></script>
(function (){
var amount_from,
rate_from,
input_amount_from = document.querySelector('#amount_from'),
input_rate_from = document.querySelector('#rate_from'),
inputs = [input_amount_from, input_rate_from],
class_amount_from = document.querySelectorAll('.amount_from'),
class_rate_from = document.querySelectorAll('.rate_from'),
class_amount_to = document.querySelectorAll('.amount_to'),
calc,
display;
calc = function () {
var rate_to = 100,
amount_to;
amount_from = parseFloat(input_amount_from.value);
rate_from = parseFloat(input_rate_from.value);
amount_to = Math.floor(amount_from * rate_from / rate_to * 100) / 100;
return amount_to;
};
display = function () {
var i,
max,
amount_to = calc();
for (i = 0, max = class_amount_from.length; i < max; i += 1) {
class_amount_from[i].innerHTML = amount_from;
}
for (i = 0, max = class_rate_from.length; i < max; i += 1) {
class_rate_from[i].innerHTML = rate_from;
}
for (i = 0, max = class_amount_to.length; i < max; i += 1) {
class_amount_to[i].innerHTML = amount_to;
}
};
inputs.forEach(function (input) {
input.onchange = display;
input.onkeyup = display;
});
display();
}());
euros at an exchange rate of is U.S. dollars.
0 コメント:
コメントを投稿