如何避免使用多个if语句来减少代码的空间、内存和时间消耗?有办法吗



我的代码相当复杂,涉及三角形分析。该代码是为用户输入每个三角形的边和底边的值而设置的。该代码首先确定这些边的值是否可以形成三角形,如果可以,然后计算三角形是锐角、钝角还是直角三角形,以及锐角、锐角、钝角还是等边三角形?!然后计算三角形的周长和面积!该代码包含73行,在我看来相当繁琐,耗费时间和空间。用更少的代码行来合理化代码编写过程,有可能实现同样的目标吗?!

代码:

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);

我将我以前的解决方案与Bankole Alex Esan建议的解决方案相结合,结果是通过将控制流和数组相结合,代码的47行明显少于以前的73行。代码如下:

var flag = false;
while(!flag){
var a = prompt('Enter side a of the triangle', a);
var b = prompt('Enter side b of the triangle', b);
var c = prompt('Enger side b of the triangle',c);
a = Number(a);
b = Number(b);
c = Number(c);
if((a>=b+c)||(b>=a+c)||(c>=a+b)){
alert('The triangle is not valid!');
continue;
}else{
alert('The triangle is valid!');
break;
}
}
const AngleTypes =  ['acute', 'obtuse','right'];
const lengthTypes = ['equilateral', 'iscosceles'];
const getTriangleAngleType = (a,b,c)=>{
if(((a**2)>(b**2)+(c**2))||((b**2)>(a**2)+(c**2))||((c**2)>(a**2)+(b**2))){
return AngleTypes[1];
}else if(((a**2)===(b**2)+(c**2))||((b**2)===(a**2)+(c**2))||((c**2)===(a**2)+  (b**2))){
return AngleTypes[2];
}else{
return AngleTypes[0];
}
}
const getTriangleType = (a,b,c)=>{
if((a===b)&&(b===c)&&(a===c)){
return lengthTypes[0];
}else if(((a===b)&&(a!==c))||((b===c)&&(b!==a))||((a===c)&&(a!==b))){
return lengthTypes[1];
}else{
return  'regular';
}
}
let AngleType = getTriangleAngleType(a, b, c);
let TriangleType = getTriangleType(a, b, c);
let P = a+b+c;
let A = Math.sqrt((P/2)*((P/2)-a)*((P/2)-b)*((P/2)-c))
console.log(`The triangle is ${AngleType} and ${TriangleType}!`); 
console.log(`The triangle perimeter is ${P}`);
console.log(`The triangle area is ${A}`);

从代码的结构来看,您似乎可以访问DOM(alert和prompt等方法是Window方法(。所以我有点想知道,当你可以只使用输入字段并输出到页面时,为什么要使用提示和警报。但没关系。

对于您的问题"用更少的代码行来合理化代码编写过程,有可能实现同样的目标吗">。简单的答案是是-当然

从你的问题来看,你的代码似乎接受了一些输入,进行了一些比较,然后根据这些比较返回结果。我在这里提供的是一些关于实现这一目标的更清洁方法的建议(因为你可能有一百万种方法可以做这样的事情(。

我们有数量有限的可能的三角形类型,通常有两类——所以我们可以把它们放在数组中。

const angleTypes = ['acute', 'obtuse', 'right'];
const lengthTypes = ['equilateral', 'iscosceles'];

然后,我们可以使用一些功能来完成一些重复的任务——下面是一些伪代码

const isValidTriangle = (a, b, c) => {
// check if entered params are valid for a triangle
// return true or false
}
const getTriangleLengthType = (a, b, c) => {
// Compare sides to get the length type
// return an index or -1 (or string of lengthTypes[index] if index > -1)
}
const getTriangleAngleType = (a, b, c) => {
// Compare sides to get the angle type
// return an index or -1 (or string of angleTypes[index] if index > -1)
}

然后我们可以使用模板文字中的函数输出结果字符串

let angleType = getTriangleAngleType(a, b, c);
let lengthType = getTriangleLengthType(a, b, c);
alert(`The triangle is ${angleType} ${lengthType ? 'and '+ lengthType:''}`)

我们还可以使用ES6缩短一些语法。例如

// to display perimeter and area of the triangle
alert(`The perimeter of the triangle is: ${a + b + c}`);
alert(`The triange area is: ${Math.sqrt((p/2)*((p/2)-a1)*((p/2)-b1)*((p/2)-c1))}`)

这些只是您可以做的几件事,以使您的代码尽可能地成为DRY(不要重复自己(。请记住伪代码,我稍后会用实际代码更新答案。如果你需要完全重写程序,我也可以提供

最新更新