Arrays - Find missing numbers in a Sequence

后端 未结 15 1391
小鲜肉
小鲜肉 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:50

    Try as shown below

    // Find the missing number
    let numArray = [0189459, 0189460, 0189461, 0189463, 0189468];
    let numLen = numArray.length;
    let actLen = Number(numArray[numLen-1])-Number(numArray[0]);
    let  allNumber = [];
    
    for(let i=0; i<=actLen; i++){
      allNumber.push(Number(numArray[0])+i);
    }
    [...allNumber].forEach(ele=>{
      if(!numArray.includes(ele)){
        console.log('Missing Number -> '+ele);
      }
    })
    

提交回复
热议问题