C6模組輸出輸入

(1)我嘗試做一個等差級數的物件模組,該物件底下包含等差級數的組成、第N項計算、從第一項到第n項之合。

ArithmeticProgression



"use strict"


class ArithmeticProgression{
      constructor(a1,d,n){
          a1=this.a1;
          d=this.d;
          n=this.n;
        var  an=this.a1+(this.n-1)*this.d;
        var  sn=(this.a1+this.an)*this.n/2;
      }
      a(n){
          return    a1+(n-1)*d;
      }
      s(n){
          return (a1+a(n))*n/2;
      }

}


module.exports=ArithmeticProgression;
````````````

Count


var a=require("./ArithmeticProgression");



a=new ArithmeticProgression(5,2,9);
b=new ArithmeticProgression(1,1,10);


console.log("the sum of a="+a.sn);
console.log("the sum of b="+b.sn);
console.log("the a4 of a="+a.a(4));
console.log("the a4 of b="+b.a(4));

```````````````

但是一如往常的遭遇錯誤。

為什麼呢?

於是我試著除錯。

results matching ""

    No results matching ""