1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
'use strict'; // 7.3なら8こドットを打ったラインを返す const getDotLineByNum = (num) => { let line = ''; for (let i = 0; i < num; i++) { line += '.'; } return line; }; // 0から1の間の数値が入った配列をグラフ化する const dispGraph = (nums) => { // 配列初期化 const a = []; for (let i = 0; i < 10; i++) { a[i] = 0; } // 割り振り for (const num of nums) { // ガード if (num < 0 || 10 <= num) { continue; } // 登録 const idx = Math.floor(num * 10); a[idx]++; } // 表示 for (let i = 0; i < a.length; i++) { const e = a[i]; const line = getDotLineByNum(e / 10); console.log(i + ' => ' + line); } }; const main = () => { const times = 10000; const nums = []; for (let i = 0; i < times; i++) { const n = Math.random(); nums.push(n); } dispGraph(nums); }; main(); |
1 2 3 4 5 6 7 8 9 10 |
0 => .................................................................................................... 1 => ...................................................................................................... 2 => ................................................................................................... 3 => ................................................................................................... 4 => ........................................................................................................ 5 => ....................................................................................................... 6 => ........................................................................................................ 7 => .............................................................................................. 8 => ..................................................................................................... 9 => ................................................................................................... |