For else loop in Javascript?

后端 未结 7 1448
我寻月下人不归
我寻月下人不归 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:16

    You can either use a boolean or you can simply return. Some thing along this line should work...

        var search = function(num){
        found = false;
        for(var i in [0,1,2,3,4]){
            if(i===num){
                console.log("Match found: "+ i);
                return true;
            }
        }
        return false;
    };
    

提交回复
热议问题