teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

余計なもの削除

2020/06/19 10:46

投稿

退会済みユーザー
answer CHANGED
@@ -168,7 +168,5 @@
168
168
  //表を生成
169
169
  data.reduce (row, document.querySelector ('#hoge tbody'));
170
170
  [["平均",...av,"--","--"]].reduce (row, document.querySelector ('#hoge tfoot'));
171
-
172
- console.log(av,std);
173
171
  </script>
174
172
  ```

2

ちょっと見直し

2020/06/19 10:46

投稿

退会済みユーザー
answer CHANGED
@@ -109,7 +109,66 @@
109
109
  data.reduce (row, document.querySelector ('#hoge tbody'));
110
110
  [["平均",...av,"--","--"]].reduce (row, document.querySelector ('#hoge tfoot'));
111
111
 
112
+ </script>
113
+
114
+ ```
115
+
116
+ 見直ししました。
117
+ 同点の場合の順番の付加方法の変更。それにともなうソートの省略。
118
+ 平均の表示を少数をまるめる。
119
+ かなり短くなりました。
120
+
121
+ ```js
122
+ <!DOCTYPE html>
123
+ <html lang="ja">
124
+ <meta charset="UTF-8">
125
+ <title>ユーザクラスの作成</title>
126
+ <style>
127
+ #hoge thead th { background: green; color: white }
128
+ #hoge tr td:first-of-type { background: rgba(0,200,0,.1)}
129
+ #hoge tr td:nth-of-type(n-1) { text-align: right; }
130
+ #hoge tfoot {background: rgba(255,0,0,.1)}
131
+ #hoge tr td:nth-of-type(n+7) { background: rgba(0,0,255,.1);}
132
+ </style>
133
+ <body>
134
+ <table border="1" id="hoge">
135
+ <thead>
136
+ <tr><th>生徒 <th>国語 <th>数学 <th>理科 <th>社会 <th>英語 <th>合計値 <th>ランク <th>偏差値
137
+ <tbody>
138
+ <tfoot>
139
+ </table>
140
+
141
+ <script>
142
+ const
143
+ data = [
144
+ ["Aさん", 80, 70, 70, 50, 60],
145
+ ["Bさん", 60, 70, 40, 80, 60],
146
+ ["Cさん", 60, 70, 70, 60, 60],
147
+ ["Dさん", 80, 40, 40, 70, 70],
148
+ ["Eさん", 70, 70, 70, 60, 70],
149
+ ["Fさん", 50, 70, 70, 60, 90],
150
+ ],
151
+ len = data.length,
152
+
153
+ sum = (a, b)=> a + b,
154
+ cell = (r, c) => (r.insertCell ().textContent = c, r),
155
+ row = (t, r) => (r.reduce (cell, t.insertRow ()), t);
156
+
157
+ //合計値を計算し配列の最後に追加、ついでに成績順追加
158
+ data.forEach (r => r.push (r.slice (1).reduce (sum)));
159
+ //平均
160
+ let av = data.reduce((a,r)=>(r.slice(1,7).forEach((r,i)=>a[i]=r+(a[i]||0)),a),[]).map(a=>(a/len).toFixed(1));
161
+ //合計値の標準偏差
162
+ let std = (data.map (r=>(r[6]-av[5])**2).reduce(sum)/len)**.5;
163
+ //成績順を付加
164
+ data.forEach (r=> r[7]= data.filter (q => r[6] < q[6]).length + 1);
165
+ //偏差値計算
166
+ data.forEach (r=> r[8] = ((r[6]-av[5])/std * 10 + 50).toFixed(2));
167
+
168
+ //表を生成
169
+ data.reduce (row, document.querySelector ('#hoge tbody'));
170
+ [["平均",...av,"--","--"]].reduce (row, document.querySelector ('#hoge tfoot'));
171
+
112
172
  console.log(av,std);
113
173
  </script>
114
-
115
174
  ```

1

蛇足

2020/06/19 10:44

投稿

退会済みユーザー
answer CHANGED
@@ -50,4 +50,66 @@
50
50
 
51
51
  </script>
52
52
 
53
+ ```
54
+ 偏差値は必要ないの?
55
+ ```html
56
+ <!DOCTYPE html>
57
+ <html lang="ja">
58
+ <meta charset="UTF-8">
59
+ <title>ユーザクラスの作成</title>
60
+ <style>
61
+ #hoge thead th { background: green; color: white }
62
+ #hoge tr td:first-of-type { background: rgba(0,200,0,.1)}
63
+ #hoge tr td:nth-of-type(n-1) { text-align: right; }
64
+ #hoge tfoot {background: rgba(255,0,0,.1)}
65
+ #hoge tr td:nth-of-type(n+7) { background: rgba(0,0,255,.1);}
66
+ </style>
67
+ <body>
68
+ <table border="1" id="hoge">
69
+ <thead>
70
+ <tr><th>生徒 <th>国語 <th>数学 <th>理科 <th>社会 <th>英語 <th>合計値 <th>ランク <th>偏差値
71
+ <tbody>
72
+ <tfoot>
73
+ </table>
74
+
75
+ <script>
76
+ const
77
+ data = [
78
+ ["Aさん", 80, 70, 70, 50, 60],
79
+ ["Bさん", 60, 70, 40, 80, 60],
80
+ ["Cさん", 60, 70, 70, 60, 60],
81
+ ["Dさん", 80, 40, 40, 70, 70],
82
+ ["Eさん", 70, 70, 70, 60, 70],
83
+ ],
84
+
85
+ sum = (a, b)=> a + b,
86
+ order = (a, b)=> a[6] !== b[6] ? a[6] < b[6]: false,
87
+ order2 = (a, b)=> a[9] > b[9],
88
+ cell = (r, c) => (r.insertCell ().textContent = c, r),
89
+ row = (t, r) => (r.reduce (cell, t.insertRow ()), t);
90
+
91
+ //合計値を計算し配列の最後に追加、ついでに成績順と連番も追加
92
+ data.forEach ((rec, idx) => rec.push (rec.slice (1).reduce (sum), 0, 0, idx));
93
+ //平均
94
+ let av = data.reduce((a,r)=>(r.slice(1,7).forEach((r,i)=>a[i]=r+(a[i]||0)),a),[]).map(a=>a/data.length);
95
+ //合計値の標準偏差
96
+ let std = (data.map (r=>(r[6]-av[5])**2).reduce(sum)/data.length)**.5;
97
+ //偏差値計算
98
+ data.forEach (rec=> rec[8] = ((rec[6]-av[5])/std * 10 +50).toFixed(2));
99
+ //成績順で並び替え
100
+ data.sort (order);
101
+ //成績順の番号を付加
102
+ data.forEach ((rec, i)=> rec[7] = i + 1);
103
+ //元に戻す
104
+ data.sort (order2);
105
+ //連番は捨てる
106
+ data.forEach (rec=> rec.pop ());
107
+
108
+ //表を生成
109
+ data.reduce (row, document.querySelector ('#hoge tbody'));
110
+ [["平均",...av,"--","--"]].reduce (row, document.querySelector ('#hoge tfoot'));
111
+
112
+ console.log(av,std);
113
+ </script>
114
+
53
115
  ```