简体   繁体   English

简化 if 语句条件

[英]Simplify if statement condition

I am making a function to check the dimensions of an item to see if it will fit in a certain box.我正在制作一个 function 来检查物品的尺寸,看它是否适合某个盒子。 The problem I am having is how long the if conditional statement is.我遇到的问题是 if 条件语句有多长。 for example;例如;

item's dimensions are 7x3x2, box's dimension are 7x5x3.物品的尺寸为 7x3x2,盒子的尺寸为 7x5x3。

if(l <= 7 && w <= 5 && h <= 3
   || l <= 7 && w <= 3 && h <= 5 
   || l <= 5 && w <= 7 && h <= 3 
   || l <= 5 && w <= 3 && h <= 7 
   || l <= 3 && w <= 5 && h <= 7 
   || l <= 3 && w <= 7 && h <= 5) {
   console.log("your item fits in this box!");
} else {
  ...
}

Is there a way to cover every possible combination instead of writing 6 different ways on the if statement?有没有办法涵盖所有可能的组合,而不是在 if 语句上写 6 种不同的方式?

Order the length, width, and height from highest to lowest first, then compare once:将长宽高从高到低排序,然后比较一次:

 const item1 = { l: 3, w: 8, h: 5 }; const item2 = { l: 2, w: 3, h: 9}; const item3 = { l: 3, w: 7, h: 5}; function orderDims(l, w, h) { const length = Math.max(l, w, h); const width = Math.max(Math.min(l, w), Math.min(Math.max(l, w), h)); const height = Math.min(l, w, h); return [length, width, height]; } function itemFits(l, w, h) { const dimArr = orderDims(l, w, h); return dimArr[0] <=7 && dimArr[1] <= 5 && dimArr[2] <= 3; } console.log(itemFits(item1['l'], item1['w'], item1['h'])); console.log(itemFits(item2['l'], item2['w'], item2['h'])); console.log(itemFits(item3['l'], item3['w'], item3['h']));

You could sort their values and compare like this:您可以对它们的值进行排序并像这样进行比较:

const x = 7;
const y = 3;
const z = 5;
console.log(JSON.stringify([x, y, z].sort()) === JSON.stringify([3, 5, 7]));

So your if statement could look like this:所以你的if语句可能是这样的:

if (JSON.stringify([l, w, h].sort()) === JSON.stringify([3, 5, 7])) {
    ...
}

You could refactor it like this:你可以像这样重构它:

 class Container { constructor(...dimensions) { this.dimensions = dimensions; } fits(l, w, h) { return JSON.stringify([l, w, h].sort()) === JSON.stringify(this.dimensions); } } const container = new Container(3, 5, 7); console.log(container.fits(3, 5, 7)); console.log(container.fits(3, 7, 5)); console.log(container.fits(5, 3, 7)); console.log(container.fits(5, 7, 3)); console.log(container.fits(7, 3, 5)); console.log(container.fits(7, 5, 3));

You can solve this question easily, if you find the volume of the item and the box and then compare the volumes.你可以很容易地解决这个问题,如果你找到物品和盒子的体积然后比较体积。

For example:例如:

item's dimensions - 7x3x2 
Box's dimensions - 7x5x3
Item volume - 42
Box volume - 105

Since volume of item is less than volume of box the, item can be fitted inside the box.由于物品的体积小于盒子的体积,所以物品可以装在盒子里。 Using only one if else you can easily solve the question.仅使用一个 if else 就可以轻松解决问题。

I think you would want to avoid extended if statements like that just for readability and code maintenance purposes?我认为您只是出于可读性和代码维护目的而希望避免这样的扩展if语句吗? You could achieve the same logic written slightly different.您可以实现略有不同的相同逻辑。 Something like (pseudocode):类似(伪代码)的东西:

let fits = true;
if (x > 7){
  fits = false;
}
if (y > 5){
  fits = false;
}
if (z > 3) {
  fits = false;
}

return fits;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM