2016-12-06 9 views
-1

私は私のベクトルオブジェクトを持っており、これをいくつかのテストに合格させようとしています。 最初に、最初のdotProduct関数は "is positive test"を渡します。 しかし、それは "否定的である"と "ゼロである"という2つの他のテストをパスするように苦労しています。Qベクトルのドットプロダクト

var Vector = (function() { 
function Vector(pX, pY, pZ) { 
    this.setX(pX); 
    this.setY(pY); 
    this.setZ(pZ); 

} 
Vector.prototype.getX = function() { 
    return this.mX; 
}; 
Vector.prototype.setX = function (pX) { 
    this.mX = pX; 
}; 
Vector.prototype.getY = function() { 
    return this.mY; 
}; 
Vector.prototype.setY = function (pY) { 
    this.mY = pY; 
}; 
Vector.prototype.setZ = function() { 
    return this.mZ; 
}; 
Vector.prototype.getZ = function (pZ) { 
    this.mZ = pZ; 
}; 
Vector.prototype.add = function (v) { 
    return new Vector(this.getX() + v.getX(), this.getY() + v.getY()); 
}; 
Vector.prototype.subtract = function (s) { 
    return new Vector(this.getX() - s.getX(), this.getY() - s.getY()); 
}; 
Vector.prototype.multiply = function (scalar) { 
    return new Vector(this.getX() * scalar, this.getY() * scalar); 
}; 
Vector.prototype.divide = function (scalar) { 
    return new Vector(this.getX()/scalar, this.getY()/scalar); 
}; 
Vector.prototype.magnitude = function() { 
    return Math.sqrt(this.getX() * this.getX() + this.getY() * this.getY()); 
}; 
Vector.prototype.normalise = function() { 
    return new Vector(this.getX()/this.magnitude(), this.getY()/this.magnitude()); 
}; 
Vector.prototype.limitTo = function (Scalar) { 
    this.normalise(); 
    this.multiply(Scalar); 
    return new Vector(this.getX(), this.getY(), this.getZ()); 
}; 

//Vector.prototype.dotProduct = function() { 
// var secondVector = new Vector(100, 400); 

// return new Vector(this.getX() * secondVector.getX() + 
//  this.getY() * secondVector.getY()); 
//}; 
Vector.prototype.dotProduct = function (secondVector) { 
    secondVector = new Vector(-100, -400); 

    return new Vector(this.getX() * secondVector.getX() + 
     this.getY() * secondVector.getY()/Math.acos(90)); 
}; 

return Vector; 

}()); 

試験条件:

describe("Dot Product", function() { 

    it("Result is zero", function() { 
     var secondVector, dotProduct; 
     secondVector = new Vector(40, -30, 0); 
     dotProduct = vector.dotProduct(secondVector); 

     expect(dotProduct).toEqual(0); 
    }); 

    it("Result is positive", function() { 
     var secondVector, dotProduct; 
     secondVector = new Vector(50, 0, 0); 
     dotProduct = vector.dotProduct(secondVector); 

     expect(dotProduct).not.toBeLessThan(0); 
    }); 

    it("Result is negative", function() { 
     var secondVector, dotProduct; 
     secondVector = new Vector(0, -50, 1); 
     dotProduct = vector.dotProduct(secondVector); 

     expect(dotProduct).toBeLessThan(0); 
    }); 
}); 

これは "この" に言及していることを最初のベクトルです。

housePosition = new Vector(150, 100); 

これは、関数が何をすべきかの説明です:

「あなたのVectorオブジェクトは、そのパラメータとして、単一のVectorオブジェクトを受け取り 『ドット積』関数を持つべき機能は、単一のスカラーを返す必要があります。 「this」VectorとパラメータVectorの内積の結果である値

私はこれについて正しく行っていますか? どうすればこれらのテストに合格することができますか?単純にベクトルを負にするか、それ以外に必要なことがあります。

+0

あなたは2つのドット積の定義を持ち、それらのそれぞれにあなたが何か(とで撮影したパラメータなし)にsecondVectorをハードcoddedていることを考えると、あなたのコードは次のようになります違う。あなたは効果的に常に0になるように設定/取得方法で問題を抱えているかもしれません。 –

+0

それらのうちの1つがコメントアウトされても、私はまだ正しく動作しています。 – Quad117

+0

それぞれにsecondVecorのハードコーディングされた値があり、入力パラメータは使用していません。つまり、これらのメソッドは決して入力に依存しません。また、あなたはsetXとgetXメソッドを表示していません。 –

答えて

0

あなたのセットとメソッドを取得するのは大丈夫です。 dotProductメソッドを見てみましょう。

Vector.prototype.dotProduct = function() { 
    var secondVector = new Vector(100, 400); 

    return new Vector(this.getX() * secondVector.getX() + 
     this.getY() * secondVector.getY() * Math.acos(90)); 
}; 

1)入力パラメータは使用しません。

2)は、乗算器

3としてハードコーディングベクトル(100、400)を使用して)それはで構成新しいベクトルオブジェクト....

this.getX() * secondVector.getX() + 
     this.getY() * secondVector.getY() * Math.acos(90) 

4の単一のパラメータを返します) Math.acos(90)はNaNを返し、cosの値は1と-1の間です。あなたが角度の余弦を得ることを意図したとしても、それはまだ間違っています。なぜなら、Math.cosの角度単位は度ではないラジアンです。

したがって、dotProduct呼び出しの結果は、mX:NaNおよびmY:未定義のVectorオブジェクトです。

Btw、dotProductのコメント付きメソッドはdotProductのように見えます。ベクトルではなく番号を返すだけです。 (少なくとも、ユニットテストでは数字を期待しています)。だから私は、あなたがこのような何かを探していると思います:

Vector.prototype.dotProduct = function (secondVector) { 
    return this.getX()*secondVector.getX() + this.getY()*secondVector.getY(); 
}; 
+0

Ah okだから、Secondvector varは必要ありません。ベクトルの代わりに値を返す必要があります。 – Quad117

+0

であり、ベクトルドット製品の正しい式が必要です。また、テストを書く前に、ブラウザ開発者コンソールを使用してコードを試してデバッグすることもできます。 –

関連する問題