Codewars Challenge - JavaScript - Find the first non-consecutive number in Array - Stack Overflow

Link to Codewars challengeThis is very basic, but for some reason, I can't figure out why I'm

Link to Codewars challenge

This is very basic, but for some reason, I can't figure out why I'm not able to return null when there are not any non-consecutive numbers in an array. My code works fine when the array is not totally consecutive:

function firstNonConsecutive(arr) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i + 1] - arr[i] !== 1) {
      return arr[i + 1];
    }
  }
  return null;
}


console.log(firstNonConsecutive([ 0, 1, 2, 3, 4, 6, 7, 8, 9 ]));

Link to Codewars challenge

This is very basic, but for some reason, I can't figure out why I'm not able to return null when there are not any non-consecutive numbers in an array. My code works fine when the array is not totally consecutive:

function firstNonConsecutive(arr) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i + 1] - arr[i] !== 1) {
      return arr[i + 1];
    }
  }
  return null;
}


console.log(firstNonConsecutive([ 0, 1, 2, 3, 4, 6, 7, 8, 9 ]));

But if the array is consecutive, i.e. like this:

function firstNonConsecutive(arr) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i + 1] - arr[i] !== 1) {
      return arr[i + 1];
    }
  }
  return null;
}

console.log(firstNonConsecutive([ 6, 7, 8, 9, 10, 11, 12 ]));

You can see that it returns undefined instead of null. Why isn't it returning null? The return is outside of the for-loop.

I tried to create an initial check to see if the array is not consecutive, like this:

function firstNonConsecutive(arr) {
  let newArr = [];
  for (let j = arr[0]; j < arr[arr.length - 1]; j++) {
    newArr.push(j);
  }
  //check if arr does not contain consecutive characters
  if (String(arr) !== String(newArr)) {
    for (let i = 0; i < arr.length; i++) {
      if (arr[i + 1] - arr[i] !== 1) {
        return arr[i + 1];
      }
    }
  }
  else {
    return null;
  }
}
console.log(firstNonConsecutive([ 0, 1, 2, 3, 4, 6, 7, 8, 9 ]));

But it did not make a difference. Any ideas?

Share Improve this question asked Feb 17, 2020 at 16:37 SpeakInCode43SpeakInCode43 5904 gold badges11 silver badges23 bronze badges 3
  • 4 i < arr.length and then arr[i + 1] will be undefined. you will want i < arr.length - 1 – Keith Commented Feb 17, 2020 at 16:40
  • You're iterating over the entire array but accessing arr[i + 1], which means the last iteration's test will always fail, then return arr[i + 1] which is undefined. – user5734311 Commented Feb 17, 2020 at 16:41
  • @Keith thanks, yeah this is the answer. – SpeakInCode43 Commented Feb 17, 2020 at 16:42
Add a ment  | 

4 Answers 4

Reset to default 5

I suggest to start from the second item and check the element and the element before.

This approach does not change the length to check for and it omits the problem to check non existent elements.

function firstNonConsecutive(arr) {
    for (let i = 1; i < arr.length; i++) {
        if (arr[i - 1] + 1 !== arr[i]) return arr[i];
    }
    return null;
}

console.log(firstNonConsecutive([0, 1, 2, 3, 4, 6, 7, 8, 9]));
console.log(firstNonConsecutive([0, 1, 2, 3, 4, 5, 6, 7, 8]));

You may try to Array.prototype.find() the gap:

const firstNonConsecutive = arr => arr.find((n,i,s) => i && n-s[i-1] > 1)

So, it's modified version that'll pass arrays without gaps and arrays with negatives, would look like that:

const src1 = [1,2,3,4,6,7,8],
      src2 = [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ],
      src3 = [ -4, -3, -2, 0, 1, 3, 4, 5 ],
      firstNonConsecutive = arr => (
        gap = arr.find((n,i,s) => i && Math.max(n,s[i-1])-Math.min(n,s[i-1]) > 1), 
        gap === undefined ? null : gap
      )
      
console.log(firstNonConsecutive(src1))
console.log(firstNonConsecutive(src2))
console.log(firstNonConsecutive(src3))
.as-console-wrapper{min-height:100%;}

While my answer may appear a bit overplicated due to Math.min()/Math.max() usage, it'll work for consecutive numbers listed in descending order just as well.

I have solution like this:

function firstNonConNum(arr) {
    const result = arr
        .find((element, i) => {
        if (i < 1) {
            return false
        }
        if ((element - arr[i - 1]) !== 1) {
            return true;
        }
    })
    if (result !== undefined) {
        return result
    }
    return null;
}

console.log(firstNonConNum([-2, 0, 1, 2, 6, 4, 5, 6, 7 ]));

This code is work, but i think, that this code is not so good.

function firstNonConsecutive (arr) {
  if (arr.length == 1 || arr.length == 0) {
    return null;
  } else { 
    let count = [];
    for (let i = 0; i < arr.length; i++){
      if (arr[i+1] - arr[i] !== 1) {
        count.push(arr[i+1])
      }
    } 
    if (count[0] == undefined) {
      return null
    } else {
      return count[0]
    }
  }
  console.log(arr);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743727679a4496896.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信