Created
May 15, 2017 18:31
-
-
Save examinedliving/92ad4ee5c11696b71d3037b003468bc3 to your computer and use it in GitHub Desktop.
Codewars Kata for Vector Class. Tests fail on auxillary values equality. Everything else works.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // codewars link=https://www.codewars.com/kata/vector-class | |
| var Vector = function(components) { | |
| this.values = components; | |
| this.result=components; | |
| return this; | |
| }; | |
| Vector.prototype.equals=function(){ | |
| return this.result; | |
| } | |
| Vector.prototype.add = function(vector) { | |
| var hisvalues = vector.values, | |
| myvalues = this.values, | |
| result; | |
| if (myvalues.length !== hisvalues.length) { | |
| throw ('Error'); | |
| } | |
| result = myvalues.map(function(e, i) { | |
| return e + hisvalues[i]; | |
| }); | |
| this.values = result; | |
| this.result=result; | |
| return this; | |
| } | |
| Vector.prototype.subtract = function(vector) { | |
| var hisvalues = vector.values, | |
| myvalues = this.values, | |
| result; | |
| if (myvalues.length !== hisvalues.length) { | |
| throw ('Error'); | |
| } | |
| result = myvalues.map(function(e, i) { | |
| return e - hisvalues[i]; | |
| }); | |
| this.values=result; | |
| this.result=result; | |
| return this; | |
| } | |
| Vector.prototype.dot = function(vector) { | |
| var hisvalues = vector.values, | |
| myvalues = this.values, | |
| result; | |
| if (myvalues.length !== hisvalues.length) { | |
| throw ('Error'); | |
| } | |
| result = myvalues.map(function(e, i) { | |
| return e * hisvalues[i]; | |
| }); | |
| this.result=eval(result.join('+')); | |
| return this.equals(); | |
| } | |
| Vector.prototype.norm=function(){ | |
| var myvalues=this.values; | |
| var result=myvalues.map(function(e,i,a){ | |
| return Math.pow(e,2); | |
| }); | |
| this.result=Math.sqrt(eval(result.join('+'))); | |
| return this.equals(); | |
| } | |
| Vector.prototype.toString=function(){ | |
| return '('+this.values.join(',')+')'; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment