回答編集履歴
3
余計なもの削除
test
CHANGED
@@ -338,10 +338,6 @@
|
|
338
338
|
|
339
339
|
[["平均",...av,"--","--"]].reduce (row, document.querySelector ('#hoge tfoot'));
|
340
340
|
|
341
|
-
|
342
|
-
|
343
|
-
console.log(av,std);
|
344
|
-
|
345
341
|
</script>
|
346
342
|
|
347
343
|
```
|
2
ちょっと見直し
test
CHANGED
@@ -220,10 +220,128 @@
|
|
220
220
|
|
221
221
|
|
222
222
|
|
223
|
+
</script>
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
```
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
見直ししました。
|
232
|
+
|
233
|
+
同点の場合の順番の付加方法の変更。それにともなうソートの省略。
|
234
|
+
|
235
|
+
平均の表示を少数をまるめる。
|
236
|
+
|
237
|
+
かなり短くなりました。
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
```js
|
242
|
+
|
243
|
+
<!DOCTYPE html>
|
244
|
+
|
245
|
+
<html lang="ja">
|
246
|
+
|
247
|
+
<meta charset="UTF-8">
|
248
|
+
|
249
|
+
<title>ユーザクラスの作成</title>
|
250
|
+
|
251
|
+
<style>
|
252
|
+
|
253
|
+
#hoge thead th { background: green; color: white }
|
254
|
+
|
255
|
+
#hoge tr td:first-of-type { background: rgba(0,200,0,.1)}
|
256
|
+
|
257
|
+
#hoge tr td:nth-of-type(n-1) { text-align: right; }
|
258
|
+
|
259
|
+
#hoge tfoot {background: rgba(255,0,0,.1)}
|
260
|
+
|
261
|
+
#hoge tr td:nth-of-type(n+7) { background: rgba(0,0,255,.1);}
|
262
|
+
|
263
|
+
</style>
|
264
|
+
|
265
|
+
<body>
|
266
|
+
|
267
|
+
<table border="1" id="hoge">
|
268
|
+
|
269
|
+
<thead>
|
270
|
+
|
271
|
+
<tr><th>生徒 <th>国語 <th>数学 <th>理科 <th>社会 <th>英語 <th>合計値 <th>ランク <th>偏差値
|
272
|
+
|
273
|
+
<tbody>
|
274
|
+
|
275
|
+
<tfoot>
|
276
|
+
|
277
|
+
</table>
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
<script>
|
282
|
+
|
283
|
+
const
|
284
|
+
|
285
|
+
data = [
|
286
|
+
|
287
|
+
["Aさん", 80, 70, 70, 50, 60],
|
288
|
+
|
289
|
+
["Bさん", 60, 70, 40, 80, 60],
|
290
|
+
|
291
|
+
["Cさん", 60, 70, 70, 60, 60],
|
292
|
+
|
293
|
+
["Dさん", 80, 40, 40, 70, 70],
|
294
|
+
|
295
|
+
["Eさん", 70, 70, 70, 60, 70],
|
296
|
+
|
297
|
+
["Fさん", 50, 70, 70, 60, 90],
|
298
|
+
|
299
|
+
],
|
300
|
+
|
301
|
+
len = data.length,
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
sum = (a, b)=> a + b,
|
306
|
+
|
307
|
+
cell = (r, c) => (r.insertCell ().textContent = c, r),
|
308
|
+
|
309
|
+
row = (t, r) => (r.reduce (cell, t.insertRow ()), t);
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
//合計値を計算し配列の最後に追加、ついでに成績順追加
|
314
|
+
|
315
|
+
data.forEach (r => r.push (r.slice (1).reduce (sum)));
|
316
|
+
|
317
|
+
//平均
|
318
|
+
|
319
|
+
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));
|
320
|
+
|
321
|
+
//合計値の標準偏差
|
322
|
+
|
323
|
+
let std = (data.map (r=>(r[6]-av[5])**2).reduce(sum)/len)**.5;
|
324
|
+
|
325
|
+
//成績順を付加
|
326
|
+
|
327
|
+
data.forEach (r=> r[7]= data.filter (q => r[6] < q[6]).length + 1);
|
328
|
+
|
329
|
+
//偏差値計算
|
330
|
+
|
331
|
+
data.forEach (r=> r[8] = ((r[6]-av[5])/std * 10 + 50).toFixed(2));
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
//表を生成
|
336
|
+
|
337
|
+
data.reduce (row, document.querySelector ('#hoge tbody'));
|
338
|
+
|
339
|
+
[["平均",...av,"--","--"]].reduce (row, document.querySelector ('#hoge tfoot'));
|
340
|
+
|
341
|
+
|
342
|
+
|
223
343
|
console.log(av,std);
|
224
344
|
|
225
345
|
</script>
|
226
346
|
|
227
|
-
|
228
|
-
|
229
347
|
```
|
1
蛇足
test
CHANGED
@@ -103,3 +103,127 @@
|
|
103
103
|
|
104
104
|
|
105
105
|
```
|
106
|
+
|
107
|
+
偏差値は必要ないの?
|
108
|
+
|
109
|
+
```html
|
110
|
+
|
111
|
+
<!DOCTYPE html>
|
112
|
+
|
113
|
+
<html lang="ja">
|
114
|
+
|
115
|
+
<meta charset="UTF-8">
|
116
|
+
|
117
|
+
<title>ユーザクラスの作成</title>
|
118
|
+
|
119
|
+
<style>
|
120
|
+
|
121
|
+
#hoge thead th { background: green; color: white }
|
122
|
+
|
123
|
+
#hoge tr td:first-of-type { background: rgba(0,200,0,.1)}
|
124
|
+
|
125
|
+
#hoge tr td:nth-of-type(n-1) { text-align: right; }
|
126
|
+
|
127
|
+
#hoge tfoot {background: rgba(255,0,0,.1)}
|
128
|
+
|
129
|
+
#hoge tr td:nth-of-type(n+7) { background: rgba(0,0,255,.1);}
|
130
|
+
|
131
|
+
</style>
|
132
|
+
|
133
|
+
<body>
|
134
|
+
|
135
|
+
<table border="1" id="hoge">
|
136
|
+
|
137
|
+
<thead>
|
138
|
+
|
139
|
+
<tr><th>生徒 <th>国語 <th>数学 <th>理科 <th>社会 <th>英語 <th>合計値 <th>ランク <th>偏差値
|
140
|
+
|
141
|
+
<tbody>
|
142
|
+
|
143
|
+
<tfoot>
|
144
|
+
|
145
|
+
</table>
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
<script>
|
150
|
+
|
151
|
+
const
|
152
|
+
|
153
|
+
data = [
|
154
|
+
|
155
|
+
["Aさん", 80, 70, 70, 50, 60],
|
156
|
+
|
157
|
+
["Bさん", 60, 70, 40, 80, 60],
|
158
|
+
|
159
|
+
["Cさん", 60, 70, 70, 60, 60],
|
160
|
+
|
161
|
+
["Dさん", 80, 40, 40, 70, 70],
|
162
|
+
|
163
|
+
["Eさん", 70, 70, 70, 60, 70],
|
164
|
+
|
165
|
+
],
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
sum = (a, b)=> a + b,
|
170
|
+
|
171
|
+
order = (a, b)=> a[6] !== b[6] ? a[6] < b[6]: false,
|
172
|
+
|
173
|
+
order2 = (a, b)=> a[9] > b[9],
|
174
|
+
|
175
|
+
cell = (r, c) => (r.insertCell ().textContent = c, r),
|
176
|
+
|
177
|
+
row = (t, r) => (r.reduce (cell, t.insertRow ()), t);
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
//合計値を計算し配列の最後に追加、ついでに成績順と連番も追加
|
182
|
+
|
183
|
+
data.forEach ((rec, idx) => rec.push (rec.slice (1).reduce (sum), 0, 0, idx));
|
184
|
+
|
185
|
+
//平均
|
186
|
+
|
187
|
+
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);
|
188
|
+
|
189
|
+
//合計値の標準偏差
|
190
|
+
|
191
|
+
let std = (data.map (r=>(r[6]-av[5])**2).reduce(sum)/data.length)**.5;
|
192
|
+
|
193
|
+
//偏差値計算
|
194
|
+
|
195
|
+
data.forEach (rec=> rec[8] = ((rec[6]-av[5])/std * 10 +50).toFixed(2));
|
196
|
+
|
197
|
+
//成績順で並び替え
|
198
|
+
|
199
|
+
data.sort (order);
|
200
|
+
|
201
|
+
//成績順の番号を付加
|
202
|
+
|
203
|
+
data.forEach ((rec, i)=> rec[7] = i + 1);
|
204
|
+
|
205
|
+
//元に戻す
|
206
|
+
|
207
|
+
data.sort (order2);
|
208
|
+
|
209
|
+
//連番は捨てる
|
210
|
+
|
211
|
+
data.forEach (rec=> rec.pop ());
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
//表を生成
|
216
|
+
|
217
|
+
data.reduce (row, document.querySelector ('#hoge tbody'));
|
218
|
+
|
219
|
+
[["平均",...av,"--","--"]].reduce (row, document.querySelector ('#hoge tfoot'));
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
console.log(av,std);
|
224
|
+
|
225
|
+
</script>
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
```
|