Home > Software engineering >  Simplify if statement condition
Simplify if statement condition

Time:12-09

I am making a function to check the dimensions of an item to see if it will fit in a certain box. The problem I am having is how long the if conditional statement is. for example;

item's dimensions are 7x3x2, box's dimension are 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?

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

I think you would want to avoid extended if statements like that just for readability and code maintenance purposes? 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;
  • Related