javascript - If else loop problem in JS?
三叔
三叔 2017-06-26 10:58:12
0
5
811

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

三叔
三叔

reply all(5)
给我你的怀抱
if(){
}else if{
}else{
}

Is this what you mean?

Ty80

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

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

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

}

学霸

Add an else if and it will be solved

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

JavaScript If...Else statement

Manual books are still a good thing!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template