My code is quite complex and involves triangle analysis. The code is set up for user to enter values of each single triangle's side and base of that values the code determines at first whether whether the triangle is possible with those sides' values if and calculates at thereafter whether the triangle is acute, obtuse or right triangle, and also whether is acute acute isosceles, obtuse isosceles or equilateral?! Thereafter it calculates the triangle's perimeter and area! The code contains 73 lines and it seems to me quite cumbersome, time and space consuming. Is it possible to achieve the same goal with fewer lines of code to rationalize code writing process?!
The code:
var flag = false;
while(!flag){
var a = prompt('Enter the first- the longest edge of the triangle(a number>=0):', a);
a1 = Number(a);
var b = prompt('Enter the second longest edge of the triangle(a number>=0):', b);
b1 =Number(b);
var c = prompt('Enter the third longest(the shortest) edge of the triangle(a number>=0):', c);
c1 = Number(c);
if((a1<=0)||(b1<=0)||(c1<=0)||(typeof a==='undefined')||(typeof b==='undefined')||(typeof c==='undefined')){
alert('Invalid date!Each of the data entered has to be a number greater than or equal to 0!');
continue;
}else if((a1>=b1+c1)||(b1>=a1+c1)||(c1>=a1+b1)){
alert('It is not a triangle! Each edge can not be greater than the sum of remaining two!');
flag =false;
}else{
alert('OK! You can move on with process!');
break;
}
}
let h1 = Math.max(a1, b1, c1);
h2 = Number(h1);
alert('The highest edge is: '+h2);
if((((a1**2)<(b1**2)+(c1**2))&&(a1===b1)&&(b1===c1)&&(a1===c1))){
alert('The triangle is equilateral!');
}else if((h2 === a1)&&((a1**2)>(b1**2)+(c1**2))&&(b1===c1)){
alert('The triangle is obtuse and isosceles!');
}else if((h2 === a1)&&((a1**2)>(b1**2)+(c1**2))){
alert('The triangle is obtuse!');
}else if((h2===a1)&&((a1**2) === (b1**2)+(c1**2))&&(b1===c1)){
alert('The triangle is right and isosceles!');
}else if((h2===a1)&&((a1**2) === (b1**2)+(c1**2))){
alert('The triangle is right!');
}else if((h2===a1)&&((a1**2)<((b1**2)+(c1**2))&&((a1===b1)||(b1===c1)||(a1===c1)))){
alert('The triangle is acute and isosceles!');
}else if((h2===a1)&&((a1**2)<(b1**2)+(c1**2))){
alert('The triangle is acute!');
}else if((h2=== b1)&&((b1**2)===(a1**2)+(c1**2))&&(a1===c1)){
alert('The triangle is right and isosceles!');
}else if((h2===b1)&&((b1**2)===(a1**2)+(c1**2))){
alert('The triangle is right!');
}else if((h2 === b1)&&((b1**2)<(a1**2)+(c1**2))&&((b1 === c1)&&(a1 === b1))&&(a1===c1)){
alert('The triangle is equilateral!');
}else if((h2 === b1)&&((b1**2)<(a1**2)+(c1**2))&&((b1 === c1)||(a1 === b1)||(a1===c1))){
alert('The triangle is acute and isosceles!');
}else if((h2 === b1)&&((b1**2)<(a1**2)+(c1**2))){
alert('The triangle is acute!');
}else if((h2 === b1)&&((b1**2)>(a1**2)+(b1**2))&&(a1===b1)){
alert('The triangle is obtuse and isosceles!');
}else if((h2 === b1)&&((b1**2)>(a1**2)+(b1**2))){
alert('The triangle is obtuse!');
}else if((h2 === c1)&&((c1**2)>(a1**2)+(b1**2))&&(a1===b1)){
alert('The triangle is obtuse and isosceles!');
}else if((h2 === c1)&&((c1**2)>(a1**2)+(b1**2))){
alert('The triangle is obtuse!');
}else if((h2 === c1)&&((c1**2)===(a1**2)+(b1**2))&&(a1===b1)){
alert('The triangle is right and isosceles!');
}else if((h2===c1)&&((c1**2)===(a1**2)+(b1**2))){
alert('The triangle is right!');
}else if((h2===c1)&&((c1**2)<(a1**2)+(b1**2))&&((a1==b1)||(b1===c1)||(a1===c1))){
alert( 'The triangle is acute and isosceles!')
}else if((h2===c1)&&((c1**2)<(a1**2)+(b1**2))){
alert('The triangle is acute!');
}else{
};
var triangle_perimeter = a1+b1+c1;
p = Number(triangle_perimeter);
alert('The perimeter of the triangle is: '+p);
var triangle_area = Math.sqrt((p/2)*((p/2)-a1)*((p/2)-b1)*((p/2)-c1));
A = Number(triangle_area);
alert('The area of the triangle is: '+ A);
Aucun commentaire:
Enregistrer un commentaire