0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

クラスを使って成績表を作る

Posted at

タイトルの通りクラスを使って成績表を作っていきます

このような形にしたいと思います
クラス表.png

上が項目名になっていて横が人、そしてランクは点数が高い順になっています

まずはテーブルタグの中身を作ります

  <table>
    <tr>
      <th>生徒</th>
      <th>国語</th>
      <th>数学</th>
      <th>理科</th>
      <th>社会</th>
      <th>英語</th>
      <th>合計値</th>
      <th>ランク</th>
    </tr>
  </table>

続いてjavascript
まずはクラス宣言の部分

  class info {//クラス宣言
    constructor(total, name, NL, math, science, society, english) {//コンストラクター
      this.total = total;
      this.name = name;
      this.NL = NL;
      this.math = math;
      this.science = science;
      this.society = society;
      this.english = english;
    }
    getTotal() {//メソッド
      return this.total = this.NL + this.math + this.science + this.society + this.english; //すべてを足し合わせたもの
    }
    getName() {
      return this.name;
    }
    getNL() {
      return this.NL;
    }
    getMath() {
      return this.math;
    }
    getScience() {
      return this.science;
    }
    getSociety() {
      return this.society;
    }
    getEnglish() {
      return this.english;
    }
  }

続いてオブジェクトの作成します

 //オブジェクトの作成
  var a = new info(this.total, 'Aさん', 80, 70, 70, 50, 60);//コンストラクターのかっこの中の順番、つまりtotal, name, NL, math, science, society, englishの順に点数を入れていく
  var b = new info(this.total, 'Bさん', 60, 70, 40, 80, 60);
  var c = new info(this.total, 'Cさん', 60, 70, 70, 60, 60);
  var d = new info(this.total, 'Dさん', 80, 40, 40, 70, 70);
  var e = new info(this.total, 'Eさん', 70, 70, 70, 60, 70);

  var arr = [ //変数arrの中にはそれぞれのトータルを入れておく
    a.getTotal(),
    b.getTotal(),
    c.getTotal(),
    d.getTotal(),
    e.getTotal(),
  ];

という形です

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?