Home > Back-end >  how to organize big conditional array
how to organize big conditional array

Time:06-11

i have to create array with a lot of conditions. but when i try to read that what i wrote it's very complicated to know what i'm try to doing. so i try to simplify to this conditions but nothing comes to my mind. placements are important too. how can i simplify this code block?

const createArrayByConditions =
 (condition1, condition2, condition3, condition4) => {

  if (condition1) {
   if (condition4) {
    return [
     1, 4, 999,
    ];
   } else {
    return [1, 999];
   }
  }

  if (condition2) {
   if (condition4) {
    return [
     2, 4, 999,
    ];
   }
   return [2, 999];
  }

  if (condition3) {
   if (condition4) {
    return [
     3, 4, 999,
    ];
   } else {
    return [3, 999];
   }
  }

  if (condition4) {
   return [4, 999];
  } else {
   return [999];
  }
 };

CodePudding user response:

A fairly clean solution comes to mind. It seems that the return array always contains 999, so we start with an array with just that value. 4 is also always included as long as condition4 is truthy. Lastly we need to prefix 1, 2, or 3, based on which conditionN evaluates as a truthy value first.

function createArrayByConditions(
  condition1, condition2, condition3, condition4
) {
  const array = [999];
  
  if (condition4) array.unshift(4);
  
  if      (condition1) array.unshift(1);
  else if (condition2) array.unshift(2);
  else if (condition3) array.unshift(3);
  
  return array;
}

CodePudding user response:

Overall it seems like there is nothing to simplify. You have your conditions and the complexity is there. If you can't make more assumptions on the returned values (they are examples, I assume), then you are good with what you have.

I personally prefer ternary operators if every conditional path eventually returns a value of the same type. But I know you can easily shoot yourself in the foot with them. However, you may decide on the readability:

const createArrayByConditions = (c1, c2, c3, c4) => c1
  ? (c4 ? [1, 4, 999] : [1, 999])
  : c2
  ? (c4 ? [2, 4, 999] : [2, 999])
  : c3
  ? (c4 ? [3, 4, 999] : [3, 999])
  : (c4 ? [4, 999] : [999]);

CodePudding user response:

This is the furthest I could simplify the conditions by looping the arguments passed as an array:

const createArrayByConditions =
 (conditions) => {
  for(let i=0;i<3;i  ){
    if ( conditions[i] && conditions[3] ){
      return [i 1, 4, 999]
    }
    else if( conditions[i] ){
      return [i 1, 999];
    }
  }  
  if(conditions[3])
    return [4, 999];
  else
    return [999];
 };

 //1-4
 console.log( createArrayByConditions([true, false, false, false]) );   //-> [1,999]
 console.log( createArrayByConditions([true, false, false, true]) );    //-> [1,4,999]

 //2-4
 console.log( createArrayByConditions([false, true, false, false]) );   //-> [2,999]
 console.log( createArrayByConditions([false, true, false, true]) );    //-> [2,4,999]

 //3-4
 console.log( createArrayByConditions([false, false, true, false]) );   //-> [3,999]
 console.log( createArrayByConditions([false, false, true, true]) );    //-> [3,4,999]
 
 //4
 console.log( createArrayByConditions([false, false, false, true]) );   //-> [4,999]
 console.log( createArrayByConditions([false, false, false, false]) );  //-> [999]

CodePudding user response:

To improve readability you can do:

const createArrayByConditions = (
  condition1,
  condition2,
  condition3,
  condition4
) => {
  switch (true) {
    case condition1:
      return condition4 ? [1, 4, 999] : [1, 999];

    case condition2:
      return condition4 ? [2, 4, 999] : [2, 999];

    case condition3:
      return condition4 ? [3, 4, 999] : [3, 999];

    case condition4:
      return [4, 999];

    default:
      return [999];
  }
};
  • Related