回答編集履歴

2

調整

2022/05/13 09:36

投稿

yambejp
yambejp

スコア116199

test CHANGED
@@ -69,6 +69,7 @@
69
69
  </tr>
70
70
  </table>
71
71
  <script>
72
+ const arr=[];
72
73
  class Score {
73
74
  constructor( name, NL, math, science, society, english) {
74
75
  this.set('name',name);
@@ -77,6 +78,7 @@
77
78
  this.set('science',science);
78
79
  this.set('society',society);
79
80
  this.set('english',english);
81
+ arr.push(this.total);
80
82
  }
81
83
  set(key,value){
82
84
  this[`_${key}`]=value;
@@ -101,8 +103,8 @@
101
103
  get science() { return this._science; }
102
104
  get society() { return this._society; }
103
105
  get english() { return this._english; }
104
- get total() { return this._NL + this._math + this._science + this._society + this._english; }
106
+ get total() { return this.NL + this.math + this.science + this.society + this.english; }
105
- get rank() { return this._rank }
107
+ get rank() { return arr.filter(x=>x>this.total).length+1 }
106
108
  }
107
109
 
108
110
  const a = new Score( 'Aさん', 80, 70, 70, 50, 60);
@@ -110,10 +112,9 @@
110
112
  const c = new Score( 'Cさん', 60, 70, 70, 60, 60);
111
113
  const d = new Score( 'Dさん', 80, 40, 40, 70, 70);
112
114
  const e = new Score( 'Eさん', 70, 70, 70, 60, 70);
113
- const arr=[a.total,b.total,c.total,d.total,e.total];
114
115
  [a,b,c,d,e].forEach(x=>{
115
- x.set('rank',arr.filter(y=>y>x.total).length+1);
116
116
  x.setTable();
117
117
  });
118
118
  </script>
119
119
  ```
120
+ ※調整しました

1

ちょうせい

2022/05/13 09:28

投稿

yambejp
yambejp

スコア116199

test CHANGED
@@ -36,7 +36,6 @@
36
36
  <td>-</td>
37
37
  </tr>`
38
38
  t1.insertAdjacentHTML('beforeend',tr);
39
- console.log(this.name);
40
39
  }
41
40
  get name() { return this._name; }
42
41
  get NL() { return this._NL; }
@@ -54,3 +53,67 @@
54
53
  const e = new Score( 'Eさん', 70, 70, 70, 60, 70);
55
54
  </script>
56
55
  ```
56
+
57
+ # rank処理つき
58
+ ```javascript
59
+ <table id="t1">
60
+ <tr>
61
+ <th>生徒</th>
62
+ <th>国語</th>
63
+ <th>数学</th>
64
+ <th>理科</th>
65
+ <th>社会</th>
66
+ <th>英語</th>
67
+ <th>合計値</th>
68
+ <th>ランク</th>
69
+ </tr>
70
+ </table>
71
+ <script>
72
+ class Score {
73
+ constructor( name, NL, math, science, society, english) {
74
+ this.set('name',name);
75
+ this.set('NL',NL);
76
+ this.set('math',math);
77
+ this.set('science',science);
78
+ this.set('society',society);
79
+ this.set('english',english);
80
+ }
81
+ set(key,value){
82
+ this[`_${key}`]=value;
83
+ }
84
+ setTable(){
85
+ const tr=`
86
+ <tr>
87
+ <td>${this.name}</td>
88
+ <td>${this.NL }</td>
89
+ <td>${this.math}</td>
90
+ <td>${this.science}</td>
91
+ <td>${this.society}</td>
92
+ <td>${this.english}</td>
93
+ <td>${this.total}</td>
94
+ <td>${this.rank}</td>
95
+ </tr>`
96
+ t1.insertAdjacentHTML('beforeend',tr);
97
+ }
98
+ get name() { return this._name; }
99
+ get NL() { return this._NL; }
100
+ get math() { return this._math; }
101
+ get science() { return this._science; }
102
+ get society() { return this._society; }
103
+ get english() { return this._english; }
104
+ get total() { return this._NL + this._math + this._science + this._society + this._english; }
105
+ get rank() { return this._rank }
106
+ }
107
+
108
+ const a = new Score( 'Aさん', 80, 70, 70, 50, 60);
109
+ const b = new Score( 'Bさん', 60, 70, 40, 80, 70);
110
+ const c = new Score( 'Cさん', 60, 70, 70, 60, 60);
111
+ const d = new Score( 'Dさん', 80, 40, 40, 70, 70);
112
+ const e = new Score( 'Eさん', 70, 70, 70, 60, 70);
113
+ const arr=[a.total,b.total,c.total,d.total,e.total];
114
+ [a,b,c,d,e].forEach(x=>{
115
+ x.set('rank',arr.filter(y=>y>x.total).length+1);
116
+ x.setTable();
117
+ });
118
+ </script>
119
+ ```