简体   繁体   English

Codewars Challenge - JavaScript - 找到数组中的第一个非连续数字

[英]Codewars Challenge - JavaScript - Find the first non-consecutive number in Array

Link to Codewars challenge 链接到 Codewars 挑战

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.这是非常基本的,但出于某种原因,我不明白为什么当数组中没有任何非连续数字时我无法返回null 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, ie 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 .您可以看到它返回undefined而不是null Why isn't it returning null?为什么不返回 null? The return is outside of the for-loop.返回在 for 循环之外。

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?有任何想法吗?

You may try to Array.prototype.find() the gap:你可以试试Array.prototype.find()的差距:

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 overcomplicated due to Math.min() / Math.max() usage, it'll work for consecutive numbers listed in descending order just as well.虽然由于Math.min() / Math.max()使用,我的答案可能看起来有点过于复杂,但它也适用于按降序列出的连续数字

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]));

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM