開発環境
- OS X Yosemite - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- SpiderMonkey (JavaScript engine)
Data Structures and Algorithms With Javascript (Michael McMillan(著)、O'Reilly Media)のChapter 2(Arrays)、Exercises 1.(No. 1820)を解いてみる。
Exercises 1.(No. 1820)
JavaScript(Emacs)
var i,
grades = {
grades: [],
add: function (grade) {
this.grades.push(grade);
},
average: function () {
var s = 0,
len = this.grades.length,
i;
for (i = 0; i < len; i += 1) {
s += this.grades[i];
}
return s / len;
}
};
for (i = 10; i <= 100; i += 10) {
grades.add(i);
}
print(grades.grades);
print(grades.average());
出力結果(Terminal, shell, SpiderMonkey)
$ js sample1.js
10,20,30,40,50,60,70,80,90,100
55
$
0 コメント:
コメントを投稿