Home > Software engineering >  how can i make all if statements in a loop ? in Javascript
how can i make all if statements in a loop ? in Javascript

Time:08-27

i've already tried a for loop but I'm new to javascript so it's all still hard for me. I am grateful for any help. It should increase values. here´s my code:

prestige_btn.addEventListener('click', function(){
if(prestige_plus == 2){
  taxi_pro_sec *= 3;
  gartenArbeit_all_five_sec *=  3; 
  zeitungAustragen *= 3;   
  mehrLimoStände  = 3;
}
if(prestige_plus == 3){
  taxi_pro_sec *= 4;
  gartenArbeit_all_five_sec *= 4; 
  zeitungAustragen *= 4;   
  mehrLimoStände  = 4;
}
if(prestige_plus == 4){
  taxi_pro_sec *= 5;
  gartenArbeit_all_five_sec *= 5; 
  zeitungAustragen *= 5;   
  mehrLimoStände  = 5;
}
if(prestige_plus == 5){
  taxi_pro_sec *= 6;
  gartenArbeit_all_five_sec *= 6; 
  zeitungAustragen *= 6;   
  mehrLimoStände  = 6;
}
if(prestige_plus == 6){
  taxi_pro_sec *= 7;
  gartenArbeit_all_five_sec *= 7; 
  zeitungAustragen *= 7;   
  mehrLimoStände  = 7;
}
if(prestige_plus == 7){
  taxi_pro_sec *= 8;
  gartenArbeit_all_five_sec *= 8; 
  zeitungAustragen *= 8;   
  mehrLimoStände  = 8;
}
}});

and so forth.

CodePudding user response:

for(const num of [2,3,4,5,6,7,8]){
    if(prestige_plus===num){
      taxi_pro_sec *= num;
      gartenArbeit_all_five_sec *= num; 
      zeitungAustragen *= num;   
      mehrLimoStände  = num;
    }
}

CodePudding user response:

You can use a loop like this:

for(const num of [2,3,4,6,7,8,9]){
    if(prestige_plus === num ) {
        taxi_pro_sec *= num   1;
        gartenArbeit_all_five_sec *= num   1;
        zeitungAustragen *= num   1;
        mehrLimoStände  = num   1;
     }
}

Answers could be better if you would describe your scenario some more.

  • Related