どうかな? ランクも追加!
html
1 <! DOCTYPE html >
2 < html lang = " ja " >
3 < meta charset = " UTF-8 " >
4 < title > ユーザクラスの作成 </ title >
5 < style >
6 #hoge thead th { background : green ; color : white }
7 #hoge tbody tr td :first-of-type { background : rgba ( 0 , 200 , 0 , .1 ) }
8 #hoge tbody tr td :nth-of-type ( n - 1 ) { text-align : right ; }
9 </ style >
10 < body >
11 < table border = " 1 " id = " hoge " >
12 < thead >
13 < tr > < th > 生徒 < th > 国語 < th > 数学 < th > 理科 < th > 社会 < th > 英語 < th > 合計値 < th > ランク
14 < tbody >
15 </ table >
16
17 < script >
18 const
19 data = [
20 [ "Aさん" , 80 , 70 , 70 , 50 , 60 ] ,
21 [ "Bさん" , 60 , 70 , 40 , 80 , 60 ] ,
22 [ "Cさん" , 60 , 70 , 70 , 60 , 60 ] ,
23 [ "Dさん" , 80 , 40 , 40 , 70 , 70 ] ,
24 [ "Eさん" , 70 , 70 , 70 , 60 , 70 ] ,
25 ] ,
26
27 sum = ( a , b ) => a + b ,
28 order = ( a , b ) => a [ 6 ] !== b [ 6 ] ? a [ 6 ] < b [ 6 ] : false ,
29 order2 = ( a , b ) => a [ 8 ] > b [ 8 ] ,
30 cell = ( r , c ) => ( r . insertCell ( ) . textContent = c , r ) ,
31 row = ( t , r ) => ( r . reduce ( cell , t . insertRow ( ) ) , t ) ;
32
33 //合計値を計算し配列の最後に追加、ついでに成績順と連番も追加
34 data . forEach ( ( rec , idx ) => rec . push ( rec . slice ( 1 ) . reduce ( sum ) , 0 , idx ) ) ;
35
36 //成績順で並び替え
37 data . sort ( order ) ;
38 //成績順の番号を付加
39 data . forEach ( ( rec , i ) => rec [ 7 ] = i + 1 ) ;
40 //元に戻す
41 data . sort ( order2 ) ;
42 //連番は捨てる
43 data . forEach ( rec => rec . pop ( ) ) ;
44
45 //表を生成
46 data . reduce ( row , document . querySelector ( '#hoge tbody' ) ) ;
47
48 </ script >
49
偏差値は必要ないの?
html
1 <! DOCTYPE html >
2 < html lang = " ja " >
3 < meta charset = " UTF-8 " >
4 < title > ユーザクラスの作成 </ title >
5 < style >
6 #hoge thead th { background : green ; color : white }
7 #hoge tr td :first-of-type { background : rgba ( 0 , 200 , 0 , .1 ) }
8 #hoge tr td :nth-of-type ( n - 1 ) { text-align : right ; }
9 #hoge tfoot { background : rgba ( 255 , 0 , 0 , .1 ) }
10 #hoge tr td :nth-of-type ( n + 7 ) { background : rgba ( 0 , 0 , 255 , .1 ) ; }
11 </ style >
12 < body >
13 < table border = " 1 " id = " hoge " >
14 < thead >
15 < tr > < th > 生徒 < th > 国語 < th > 数学 < th > 理科 < th > 社会 < th > 英語 < th > 合計値 < th > ランク < th > 偏差値
16 < tbody >
17 < tfoot >
18 </ table >
19
20 < script >
21 const
22 data = [
23 [ "Aさん" , 80 , 70 , 70 , 50 , 60 ] ,
24 [ "Bさん" , 60 , 70 , 40 , 80 , 60 ] ,
25 [ "Cさん" , 60 , 70 , 70 , 60 , 60 ] ,
26 [ "Dさん" , 80 , 40 , 40 , 70 , 70 ] ,
27 [ "Eさん" , 70 , 70 , 70 , 60 , 70 ] ,
28 ] ,
29
30 sum = ( a , b ) => a + b ,
31 order = ( a , b ) => a [ 6 ] !== b [ 6 ] ? a [ 6 ] < b [ 6 ] : false ,
32 order2 = ( a , b ) => a [ 9 ] > b [ 9 ] ,
33 cell = ( r , c ) => ( r . insertCell ( ) . textContent = c , r ) ,
34 row = ( t , r ) => ( r . reduce ( cell , t . insertRow ( ) ) , t ) ;
35
36 //合計値を計算し配列の最後に追加、ついでに成績順と連番も追加
37 data . forEach ( ( rec , idx ) => rec . push ( rec . slice ( 1 ) . reduce ( sum ) , 0 , 0 , idx ) ) ;
38 //平均
39 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 ) ;
40 //合計値の標準偏差
41 let std = ( data . map ( r => ( r [ 6 ] - av [ 5 ] ) ** 2 ) . reduce ( sum ) / data . length ) ** .5 ;
42 //偏差値計算
43 data . forEach ( rec => rec [ 8 ] = ( ( rec [ 6 ] - av [ 5 ] ) / std * 10 + 50 ) . toFixed ( 2 ) ) ;
44 //成績順で並び替え
45 data . sort ( order ) ;
46 //成績順の番号を付加
47 data . forEach ( ( rec , i ) => rec [ 7 ] = i + 1 ) ;
48 //元に戻す
49 data . sort ( order2 ) ;
50 //連番は捨てる
51 data . forEach ( rec => rec . pop ( ) ) ;
52
53 //表を生成
54 data . reduce ( row , document . querySelector ( '#hoge tbody' ) ) ;
55 [ [ "平均" , ... av , "--" , "--" ] ] . reduce ( row , document . querySelector ( '#hoge tfoot' ) ) ;
56
57 </ script >
58
見直ししました。
同点の場合の順番の付加方法の変更。それにともなうソートの省略。
平均の表示を少数をまるめる。
かなり短くなりました。
js
1 < ! DOCTYPE html >
2 < html lang = "ja" >
3 < meta charset = "UTF-8" >
4 < title > ユーザクラスの作成 < / title >
5 < style >
6 #hoge thead th { background : green ; color : white }
7 #hoge tr td : first - of - type { background : rgba ( 0 , 200 , 0 , .1 ) }
8 #hoge tr td : nth - of - type ( n - 1 ) { text - align : right ; }
9 #hoge tfoot { background : rgba ( 255 , 0 , 0 , .1 ) }
10 #hoge tr td : nth - of - type ( n + 7 ) { background : rgba ( 0 , 0 , 255 , .1 ) ; }
11 < / style >
12 < body >
13 < table border = "1" id = "hoge" >
14 < thead >
15 < tr > < th > 生徒 < th > 国語 < th > 数学 < th > 理科 < th > 社会 < th > 英語 < th > 合計値 < th > ランク < th > 偏差値
16 < tbody >
17 < tfoot >
18 < / table >
19
20 < script >
21 const
22 data = [
23 [ "Aさん" , 80 , 70 , 70 , 50 , 60 ] ,
24 [ "Bさん" , 60 , 70 , 40 , 80 , 60 ] ,
25 [ "Cさん" , 60 , 70 , 70 , 60 , 60 ] ,
26 [ "Dさん" , 80 , 40 , 40 , 70 , 70 ] ,
27 [ "Eさん" , 70 , 70 , 70 , 60 , 70 ] ,
28 [ "Fさん" , 50 , 70 , 70 , 60 , 90 ] ,
29 ] ,
30 len = data . length ,
31
32 sum = ( a , b ) => a + b ,
33 cell = ( r , c ) => ( r . insertCell ( ) . textContent = c , r ) ,
34 row = ( t , r ) => ( r . reduce ( cell , t . insertRow ( ) ) , t ) ;
35
36 //合計値を計算し配列の最後に追加、ついでに成績順追加
37 data . forEach ( r => r . push ( r . slice ( 1 ) . reduce ( sum ) ) ) ;
38 //平均
39 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 ) ) ;
40 //合計値の標準偏差
41 let std = ( data . map ( r => ( r [ 6 ] - av [ 5 ] ) ** 2 ) . reduce ( sum ) / len ) ** .5 ;
42 //成績順を付加
43 data . forEach ( r => r [ 7 ] = data . filter ( q => r [ 6 ] < q [ 6 ] ) . length + 1 ) ;
44 //偏差値計算
45 data . forEach ( r => r [ 8 ] = ( ( r [ 6 ] - av [ 5 ] ) / std * 10 + 50 ) . toFixed ( 2 ) ) ;
46
47 //表を生成
48 data . reduce ( row , document . querySelector ( '#hoge tbody' ) ) ;
49 [ [ "平均" , ... av , "--" , "--" ] ] . reduce ( row , document . querySelector ( '#hoge tfoot' ) ) ;
50 < / script >
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/18 03:27
退会済みユーザー
2020/06/18 04:46
2020/06/18 05:12
退会済みユーザー
2020/06/18 15:52