Skip to content

Commit e62252c

Browse files
committed
ランキングの実装完了
1 parent 9f11a5c commit e62252c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

app.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
11
'use strict';
2+
const fs = require('fs');
3+
const readline = require('readline');
4+
const rs = fs.ReadStream('./popu-pref.csv');
5+
const rl = readline.createInterface({ 'input':rs, 'output': {}});
6+
const map = new Map(); // key: 都道府県 value: 集計データのオブジェクト
7+
rl.on('line', (lineString) => {
8+
const columns = lineString.split(',');
9+
const year = parseInt(columns[0]);
10+
const prefecture = columns[2];
11+
const popu = parseInt(columns[7]);
12+
if (year === 2010 || year ===2015) {
13+
let value =map.get(prefecture);
14+
if (!value) {
15+
value = {
16+
popu10: 0,
17+
popu15: 0,
18+
change: null
19+
};
20+
}
21+
if (year === 2010) {
22+
value.popu10 += popu;
23+
}
24+
if (year === 2015) {
25+
value.popu15 += popu;
26+
}
27+
map.set(prefecture, value);
28+
}
29+
});
30+
rl.resume();
31+
rl.on('close', () => {
32+
for (let pair of map) {
33+
const value = pair[1];
34+
value.change = value.popu15 / value.popu10;
35+
}
36+
const rankingArray = Array.from(map).sort((pair1, pair2) => {
37+
return pair2[1].change -pair1[1].change;
38+
});
39+
const rankingStrings = rankingArray.map((pair) => {
40+
return pair[0] + ': ' + pair[1].popu10 + '=>' + pair[1].popu15 + '変化率:' + pair[1].change;
41+
});
42+
console.log(rankingStrings);
43+
});

0 commit comments

Comments
 (0)