For else loop in Javascript?

后端 未结 7 1326
我寻月下人不归
我寻月下人不归 2021-02-03 20:47

Is there a Javascript equivalent of the python \'for-else\' loop, so something like this:

searched = input(\"Input: \");
for i in range(5):
    if i==searched:
          


        
7条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-03 21:23

    Working example (you need to use the flag):

    var search = function(num){
        var found = false;
        for(var i=0; i<5; i++){
            if(i===num){
                console.log("Match found: "+ i);
                found = true;
                break;
            }
        }
        if(!found){
            console.log("No match found!");
        }
    };
    

提交回复
热议问题