【发布时间】:2016-12-06 21:59:43
【问题描述】:
我有我的矢量对象,并试图让它通过一些测试。 首先,第一个 dotProduct 函数通过了“是肯定的测试”。 但我正在努力使其通过“为阴性”和“为零”的另外两个测试。
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);
});
});
这是“this”所指的第一个向量。
housePosition = new Vector(150, 100);
这是函数应该做什么的描述:
"你的 Vector 对象应该有一个 'dotProduct' 函数,该函数将单个 Vector 对象作为其参数。该函数应该返回一个标量值,它是 'this' Vector 和参数 Vector 的点积的结果"
我这样做对吗? 我怎样才能让它通过这些测试?只需将向量设为负数,或者我需要做些什么。
【问题讨论】:
-
考虑到您有 2 个 dotProduct 定义,并且在每个定义中您都有 secondVector 硬编码(并且没有传入参数),您的代码可能是错误的。您可能在 set/get 方法中遇到问题,这些问题实际上会使您的计算始终为 0。
-
即使其中一个被注释掉了,我仍然只有正确工作的“积极”。
-
在它们中的每一个中,您都有 secondVecor 的硬编码值,并且您没有使用任何输入参数。照原样,这些方法不以任何方式依赖于输入。此外,您没有显示您的 setX 和 getX 方法。
-
现在添加了我的整个矢量对象
-
他们接受了使用 chutzpah 扩展的测试,它使用 jasmine
标签: javascript vector jasmine dot-product