-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo23.js
138 lines (122 loc) · 4.24 KB
/
demo23.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// function Vector(components) {
// // TODO: Finish the Vector class.
// this.arr = components;
// this.add = add;
// this.subtract=subtract;
// this.norm=norm;
// this.dot=dot;
// }
// function add(aa) {
// if(this.arr.length === aa.arr.length) {
// var result=[];
// for(var i=0; i<this.arr.length; i++) {
// result.push(this.arr[i]+aa.arr[i]);
// }
// return new Vector(result);
// } else {
// throw 'throws an error';
// }
// }
// function subtract(aa) {
// if(this.arr.length === aa.arr.length) {
// var result=[];
// for(var i=0; i<this.arr.length; i++) {
// result.push(this.arr[i]-aa.arr[i]);
// }
// return new Vector(result);
// } else {
// throw 'throws an error';
// }
// }
// function dot(aa) {
// if(this.arr.length === aa.arr.length) {
// var result=0;
// for(var i=0; i<this.arr.length; i++) {
// result=result+this.arr[i]*aa.arr[i];
// }
// return result;
// } else {
// throw 'throws an error';
// }
// }
// function norm() {
// var result=0;
// for(var i=0; i<this.arr.length; i++) {
// result=result+this.arr[i]*this.arr[i];
// }
// return Math.sqrt(result);
// }
// Vector.prototype.equals = function(s){
// return this.arr.join('')==s.arr.join('');
// }
// Vector.prototype.toString=function(){
// return '('+this.arr.toString()+')';
// }
// Create a Vector object that supports addition, subtraction, dot products, and norms. So, for example:
// var a = new Vector([1,2,3]);
// var b = new Vector([3,4,5]);
// var c = new Vector([5,6,7,8]);
// a.add(b); // should return Vector([4,6,8])
// a.subtract(b); // should return Vector([-2,-2,-2])
// a.dot(b); // should return 1*3+2*4+3*5 = 26
// a.norm(); // should return sqrt(1^2+2^2+3^2)=sqrt(14)
// a.add(c); // throws an error
// If you try to add, subtract, or dot two vectors with different lengths, you must throw an error!
// Also provide:
// a toString function, so that using the vectors from above, a.toString() === '(1,2,3)' (in Python, this is a __str__ method, so that str(a) == '(1,2,3)')
// an equals function, so that two vectors who have the same components are equal
// The test cases will utilize the user-provided equals method.
// https://www.codewars.com/kata/526dad7f8c0eb5c4640000a4/solutions/javascript
// var Vector = function (components) {
// this.x = components;
// };
// Vector.prototype.add = function(b){
// var a = this.x
// b = b.x;
// if(a.length !== b.length) throw new TypeError("Vectors have different length");
// return new Vector(a.map(function(x,i){ return x + b[i]; }));
// }
// Vector.prototype.subtract = function(b){
// var a = this.x;
// b = b.x;
// if(a.length !== b.length) throw new TypeError("Vectors have different length");
// return new Vector(a.map(function(x,i){ return x - b[i]; }));
// }
// Vector.prototype.dot = function(b){
// var a = this.x;
// b = b.x;
// if(a.length !== b.length) throw new TypeError("Vectors have different length");
// return a.reduce(function(s,x,i){ return s + x * b[i]; },0);
// }
// Vector.prototype.equals = function(b){
// var a = this.x;
// b = b.x;
// if(a.length !== b.length) return false;
// return a.every(function(x,i){ return x === b[i]; });
// }
// Vector.prototype.norm = function(){
// var a = this.x;
// return Math.sqrt(a.reduce(function(s,x){ return s + x*x }, 0));
// }
// Vector.prototype.toString = function(){
// return '(' + this.x.join(',') + ')';
// }
// var Vector = function (components) {
// this.items = components;
// this.length = components.length;
// };
// Vector.prototype = {
// do: function (action, vector) {
// if (vector.length !== this.length) { throw 'Different Length!'; }
// return new Vector(this.items.map(function (v, k) {
// return eval(v + action + vector.items[k])
// }));
// },
// add: function (v) { return this.do('+', v); },
// subtract: function (v) { return this.do('-', v); },
// sum: function (v) { return eval(v.items.join('+')); },
// dot: function (v) { return this.sum(this.do('*', v)); },
// norm: function () { return Math.sqrt(this.dot(this)); },
// toString: function() { return '(' + this.items + ')'; },
// equals: function (v) { return this.toString() == v.toString(); }
// };