For else loop in Javascript?

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

    If your ultimate is goal is to check whether given input is there are not in an array, you can simply make use of indexOf function.

    var arr = [1,2,3,4,5]
    var lookingFor = 5 ; 
    
    if ( arr.indexOf(lookingFor) > -1 ) {
      console.log("Input is here") ;
    } else {
      console.log("Nope");
    }
    
    

    https://jsfiddle.net/7wnasv5e/

提交回复
热议问题