Home  >  Q&A  >  body text

javascript - If else loop problem in JS?

I wrote a code before, the general logic is as follows


    function control (type) {
    if (type == 1){
     console.log("功能1");
    
    }else {
    console.log("功能2");
    
    }
    
    }

Because the previous business logic requirements only have function 1 and function 2, function 1 will be executed when control(1) is executed, and function 2 will be executed for the rest

Now the requirements have been changed and a function 3 needs to be added because the previous logic was complicated and I did not want to change the previous logic nesting

function control (type) {
if (type == 1){
 console.log("功能1");

}else {
console.log("功能2");

}
 if(type == 3){
 console.log("功能3");

}

}
control(3);

In this case, function 3 and function 2 are executed together. How can I only execute function 3

No need to switch case

三叔三叔2672 days ago780

reply all(5)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-06-26 11:00:12

    if(){
    }else if{
    }else{
    }

    Is this what you mean?

    reply
    0
  • PHP中文网

    PHP中文网2017-06-26 11:00:12

    if (type == 1){
    console.log("function 1");

    }else if(type == 3) {
    console.log("function 3");

    }
    else{
    console.log("Function 2");

    }

    reply
    0
  • 欧阳克

    欧阳克2017-06-26 11:00:12

    Add an else if and it will be solved

    reply
    0
  • 三叔

    三叔2017-06-26 11:00:12

    function control (type) {
        if (type == 1){
         console.log("功能1");
        
        } else if (type == 3){
         console.log("功能3");
        
        } else {
         console.log("功能2");
        
        }
    }
    control(3);

    reply
    0
  • 学习ing

    学习ing2017-06-26 11:00:12

    JavaScript If...Else statement

    Manual books are still a good thing!

    reply
    0
  • Cancelreply