-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangle.js
More file actions
297 lines (262 loc) · 7.47 KB
/
Copy pathtriangle.js
File metadata and controls
297 lines (262 loc) · 7.47 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/************************************************************************** */
// definition of the sides
/* export class Triangle {
constructor(s1,s2,s3) {
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
}
kind (){
const noWay = (s1, s2, s3)=>{
// sides can't be zero
if( s1 === 0 || s2 === 0 || s3 === 0){
return true;
//stricly 0
}
//sides can't be negative
else if(s1 < 0 || s2 < 0 || s3 < 0){
return true;
}
// sum of 2 sides <= 3rd side
if(s1 + s2 <= s3){
return true;
}
//same to other sides
else if( s1 + s3 <= s2){
return true;
}
else if(s2 + s3 <= s1){
return true;
}
//otherwise
return false;
// how could it be with async/await? so I can catch the errors
};
const equilateral = (s1, s2, s3)=>{
//all sides equals
if (s1 === s2 && s1 === s3){
console.log(`true`);
return true;
}
console.log(`false`);
return false;
};
const isosceles =(s1, s2, s3)=>{
// 2 equal sides
if( s1 === s2 || s1 === s3 || s2 === s3){
console.log(`true`);
return true;
}
console.log(`false`);
return false;
};
const scalene = (s1, s2, s3) =>{
// no equal sides
if(s1 === s2) { return false;}
if(s1 === s3) { return false;}
if(s2 === s3) { return false;}
else {
console.log(`scalene`);
return true;}
};
if(noWay(this.s1, this.s2, this.s3)){
throw error `Numbers don't match the rules`;
}
else if(equilateral(this.s1, this.s2, this.s3)){
console.log( `This is an eqilateral triangle`);
return `equilateral`;
}
else if(isosceles(this.s1, this.s2, this.s3)){
console.log( `This is an isosceles triangle`);
return `isosceles`;
}
else if(scalene(this.s1, this.s2, this.s3)){
console.log( `This is an scalene triangle`);
return `scalene`
};
};
}; */
/*****************************************************************************************/
/* export class Triangle {
constructor(a, b, c) {
this.a = a;
this.b = b;
this.c = c;
}
kind(side1, side2, side3) {
if( side1 === 0 || side2 === 0 || side3 === 0){
return'Need positive numbers';
}else if( side1 < 0 || side2 < 0 || side3 < 0){
return 'no way';
}else if(this.a + this.b <= this.c || this.a + this.c <= this.b || this.b +
this.c <= this.a) {
return "illegal";
} if(side1 === side2 && side1 === side3) {
return 'equilateral'
} else if (side1 === side2 || side1 === side3 || side2 === side3) {
return 'isosceles'
}
else{
return 'scalene';
}
};
}; */
/*******************************************************************************************/
/*********************************************************************************** */
/* export class Triangle {
constructor(a, b, c) {
// Declaration sides
this.sides = [a, b, c];//array sides
// new array. How.? expand (doesn't modify original array)and set
this.onlySides = [...new Set(this.sides)] ;
}
kind () {
const inequalSides = ()=>{
const sides = this.sides;
const length = sides.length;
for (let i = 0; i < length; i++){
let sum;
if( i === length - 1){
sum = sides[0] +sides[1];
}else if (i === length - 2){
sum = sides[0] + sides[i + 1];
}else{
sum = sides[i + 1] + sides[i + 2];
}if(sides[i] >sum){
return true;
}
}
}
const checkValid = ()=>{
const noValid = this.onlySides.some((sideLength) => sideLength <=0);
if (noValid)
throw 'We are going to need more than zero';
if (inequalSides())
throw 'illegal';
}
let equilateral = ()=>{
return this.onlySides.length === 1;
}
let isosceles = ()=>{
return this.onlySides.length === 2;
}
checkValid();
if(equilateral())
return 'equilateral';
if(isosceles())
return 'isosceles';
return 'scalene';
};
}; */
/********************************************************************************************************/
/* export class Triangle {
constructor(a, b, c) {
this.sides = [a, b, c].sort((a, b) => a - b)
}
kind() {
const [short, med, long] = this.sides
const positive = short > 0
const passesEquality = long - med - short < 0
if (!positive || !passesEquality) {
throw new RangeError('Sides are not valid')
}
switch (new Set(this.sides).size) {
case 1: return 'equilateral';
case 2: return 'isosceles';
case 3: return 'scalene'
}
};
}; */
/********************************************************************************************** */
/* const rules = [
{
rule: ({a,b,c}) => a === 0 || b === 0 || c === 0,
type: () => { throw new Error('Triangle can\'t have 0 size side') }
},
{
rule: ({a,b,c}) => a <= 0 || b <= 0 || c <= 0,
type: () => { throw new Error('Triangle can\'t have negative or zero side') }
},
{
rule: ({a,b,c}) => a + b <= c || a + c <= b || b + c < a,
type: () => { throw new Error('Triangle is impossible') }
},
{
rule: ({a,b,c}) => a === b && a === c,
type: () => 'equilateral'
},
{
rule: ({a,b,c}) => a === b || a === c || b === c,
type: () => 'isosceles'
},
{
rule: ({a,b,c}) => a !== b && a !== c && b !== c,
type: () => 'scalene'
},
{
rule: () => true,
type: () => { throw new Error('We\'re missing a rule') }
},
]
export class Triangle {
constructor(a,b,c){
this.sides = {a,b,c}
}
kind() {
return rules
.find(({rule}) => rule(this.sides))
.type()
};
}; */
/****************************************************************************************/
/* export class Triangle {
constructor(a, b, c) {
this.sides = [a, b, c].sort((a ,b) => a - b);
this.sideSet = new Set (this.sides)
}
kind () {
//const [a,b,c] = this.sides;
//const zeroSides = a * b * c === 0;
const lessSides = a < 0;
const doubleLess = a + b < c;
for(let side of this.sideSet){
if(side <=0){
throw 'We are going to need more than zero';
}
};
//if(zeroSides)
//throw 'We are going to need more than zero';
if(lessSides)
throw 'no way';
if(doubleLess)
throw "illegal";
if(a === b && a === c)
return 'equilateral'
if (a === b || a === c || b === c)
return 'isosceles';
return 'scalene';
};
};*/
/*****************************************************************/
export class Triangle {
constructor(...sides) {
this.sides = sides.sort((a ,b) => a - b);
this.sideSet = new Set(this.sides);
}
kind () {
const [a,b,c] = this.sides;
for(let side of this.sideSet){
if(side <= 0 || a + b < c){
throw 'No way'
}
};
switch (this.sideSet.size){
case 1:
return 'equilateral';
case 2:
return 'isosceles';
case 3:
return 'scalene';
}
};
};