1

In the below code, there are 3 methods: add, dot and equals. The add and dot methods calculate the addition and dot product of two vectors. The equals method checks that the return values of the add and dot methods match the argument passed to the equals method.

I'm getting the error TypeError: a.dot(...).equals is not a function if I try to invoke the equals method on the dot method. What am I doing wrong and how can I get this to work? There are no errors when I invoke the equals method on the add method.

function Vector(vector){

      this.vector=vector;
      }

      //add method
      Vector.prototype.add= function(b){
        var res=[];
        var vec1 =this["vector"];
        var vec2 = b["vector"];
        for (i=0; i<vec1.length; i++){
          res.push(vec1[i]+vec2[i]);
        }
        var result = new Vector(res);
        return result;
      };
      //dot product method
      Vector.prototype.dot= function(b){
        var res=0;
        var vec1 =this["vector"];
        var vec2 = b["vector"];
        for (i=0; i<vec1.length; i++){
          res+=vec1[i]*vec2[i];
        }
        return res;
      };
      //equals method
      Vector.prototype.equals = function(answer){
        if (this.toString()===answer.toString()){
          return true;
        } else {
        console.log(this);
            return false;
        }
      };
    //test
    var a = new Vector([1,2]);
    var b = new Vector([3,4]);
    a.add(b).equals(new Vector([4,6]));
    a.dot(b).equals(11);
T S
  • 31
  • 1
  • 3

2 Answers2

1

The dot method returns a Number, not a Vector. Try using == instead of equals.

Eric Galluzzo
  • 3,191
  • 1
  • 20
  • 20
0

Eric Galluzzo is correct, but I thought I'd give more explanation, since I'm aware of the background (for the interested reader, this comes from a Codewars exercise: http://www.codewars.com/kata/vector-class/train/javascript).

The way you have written the method means that a.dot(b).equals(11) will compare the string representation of a Vector object with the string representation of 11. These will clearly not be equal, since you will be comparing something like 'function Vector(vector) {...}'=='11'.

I think you have misunderstood the purpose of the equals method. It is the specific implementation of an equality check between objects of the Vector class only. When comparing built-in types you should use == or === if possible.

The description in the kata linked above says "The test cases will utilize the user-provided equals method", but the author means this only when making comparisons between Vector objects. When comparing the result of the dot or norm methods, the equality operators are used.

See this SO question for more about comparing objects. It also gives you some idea of why comparing the string representation of a whole object is not always a great idea.

Community
  • 1
  • 1
em-12
  • 1
  • 1
  • 2
  • I didn't realise the equals method should only be used for add and subtract. Thank you both – T S Dec 03 '15 at 19:16