開発環境
- 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 4(Making Decisions)、17(Blood Alcohol Calculator)を取り組んでみる。
17(Blood Alcohol Calculator)
コード(Emacs)
<label for="weight0">
Body weight in pounds:
</label>
<input id="weight0" type="number" min="0">
<br>
<label for="gender0">
Gender:
</label>
<input id="men0" type="radio" name="gender" checked>
<label for="men0">Men</label>
<input id="women0" type="radio" name="gender">
<label for="women0">Woman</label>
<br>
<label for="total0">
total alcohol consumed, in onces (oz):
</label>
<input id="total0" type="number" min="0">
<br>
<label for="ratio0">
The alcohol distribution ratio:
</label>
<input id="ratio0" type="number" min="0">
<br>
<label for="hours0">
Number of hours since the last drink:
</label>
<input id="hours0" type="number" min="0">
<br>
<br>
<div id="output0"></div>
<script src="sample17.js"></script>
(function () {
'use strict';
var bac,
LEGAL_BAC = 0.08,
input_total = document.querySelector('#total0'),
input_gender = document.querySelector('#men0'),
input_weight = document.querySelector('#weight0'),
input_ratio = document.querySelector('#ratio0'),
input_hours = document.querySelector('#hours0'),
inputs = [input_total, input_gender, input_weight, input_ratio,
input_hours],
div_output = document.querySelector('#output0'),
calc,
display;
calc = function () {
var a = parseFloat(input_total.value),
w = parseFloat(input_weight.value),
r,
h = parseFloat(input_hours.value);
if (input_gender.checked) {
r = 0.73;
} else {
r = 0.66;
}
return (a * 5.14 / w * r) - 0.015 * h;
};
display = function () {
var bac = calc();
div_output.innerHTML =
'Your BAC is ' + bac + '<br>';
if (bac < LEGAL_BAC) {
div_output.innerHTML +=
'It is legal for you to drive.';
} else {
div_output.innerHTML +=
'It is not legal for you to drive.';
}
};
inputs.forEach(function (input) {
input.onkeyup = display;
input.onchange = display;
});
display();
}());
0 コメント:
コメントを投稿