Vector class

by HYEONGJINKIM

JavaScript

var Vector = function (components) {
  // TODO: Finish the Vector class.  
  this.components = components;
  return this;
};

Vector.prototype.add = function(other){
    var result = [];
    
    if(other && other.size() === this.components.length){
        for(var i =0, length = this.components.length; i<length; i++){            
            result[i] = this.components[i] + other.components[i];
        }
        return new Vector(result);
    }else{
        throw Error('error');
    }
}

Vector.prototype.equals = function(other){    
    return this.toString() === other.toString();
}

Vector.prototype.toString = function(){
    return '('+this.components.join(',')+')'
}

Vector.prototype.size = function(){
    return this.components.length;
};

var a = new Vector([1,2]);
var b = new Vector([3,4]);

a.add(b).equals(new Vector([4,6]))