jeudi 24 juin 2021

How to reduce multiple if statements complexity

I feel like the 5 if statements can be done with one line of code or fewer lines. I was thinking of using the forEach method but I can not wrap my mind around improving this correctly.

var previous_stays = 12,
    booking_agency_id = null,
    reservation_spend = 375.95,
    room_spend = [12.23,2.35,null,17.99],
    reservation_nights = 4,
    gold_tier = 0;

while (gold_tier == 0) {
 a = (previous_stays >= 5);
 b = !booking_agency_id;
 c = reservation_spend;
 d = reservation_nights;

 if (room_spend.length > 0) {
     c = c + room_spend[0];
     if (room_spend.length > 1) {
         c = c + room_spend[1];
         if (room_spend.length > 2) {
             c = c + room_spend[2];
             if (room_spend.length > 3) {
                 c = c + room_spend[3];
                 if (room_spend.length > 4) {
                     c = c + room_spend[4];
                     if (room_spend.length > 5) {
                         c = c + room_spend[5];
                     }
                 }
             }
         }
     }
 }

 if (!(((c/d) > 250) && a && b)) {
   break;
 }

 gold_tier = 1;
}

Aucun commentaire:

Enregistrer un commentaire