Arrays - Find missing numbers in a Sequence

后端 未结 15 1381
小鲜肉
小鲜肉 2021-02-01 04:08

I\'m trying to find an easy way to loop (iterate) over an array to find all the missing numbers in a sequence, the array will look a bit like the one below.

var nu

15条回答
  •  情歌与酒
    2021-02-01 04:54

    const findMissing = (numarr) => {
      for(let i = 1; i <= numarr.length; i++) {
          if(i - numarr[i-1] !== 0) {
            console.log('found it', i)
            break;
          } else if(i === numarr.length) console.log('found it', numarr.length + 1)
        }
      };
    
    console.log(findMissing([1,2,3,4,5,6,7,8,9,10,11,12,13,14]))
    

提交回复
热议问题