開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- SpiderMonkey, Node.js(V8) (JavaScript engine)
Data Structures and Algorithms With Javascript (Michael McMillan(著)、O'Reilly Media)のChapter 14(Advanced Algorithms)、Exercises 3.(No. 6553)を解いてみる。
Exercises 3.(No. 6553)
JavaScript(Emacs)
/*jslint browser : true, continue : true,
devel : true, indent : 4, maxerr : 50,
newcap : true, nomen : true, plusplus : false,
regexp : true, sloppy : true, vars : false,
white : true
*/
/*global */
var orig_amt = 0.3,
coins = [],
makeChange,
showChange;
makeChange = function (orig_amt, coins) {
var remain_amt = 0;
if (orig_amt % 0.25 < orig_amt) {
coins[2] = parseInt(orig_amt / 0.25, 10);
remain_amt = orig_amt % 0.25;
orig_amt = remain_amt;
}
if (orig_amt % 0.05 < orig_amt) {
coins[1] = parseInt(orig_amt / 0.05, 10);
remain_amt = orig_amt % 0.05;
orig_amt = remain_amt;
}
coins[0] = parseInt(orig_amt / 0.01, 10);
};
showChange = function (coins) {
if (coins[2] > 0) {
console.log('Number of quarters - ' + coins[2] + ' - ' +
coins[2] * 0.25);
}
if (coins[1] > 0) {
console.log('Number of nickles - ' + coins[1] + ' - ' +
coins[1] * 0.05);
}
if (coins[0] > 0) {
console.log('Number of pennies - ' + coins[0] + ' - ' +
coins[0] * 0.01);
}
};
makeChange(orig_amt, coins);
showChange(coins);
出力結果(Terminal, shell, SpiderMonkey)
$ jslint sample3.js
sample3.js is OK.
$ node sample3.js
Number of quarters - 1 - 0.25
Number of pennies - 4 - 0.04
$
0 コメント:
コメントを投稿