你能帮我理解代码吗?任务是找到第一个非连续数
Coud you help me understand the code ? the task is to find first non consecutive number
const firstNonConsecutive = arr => arr.find((number, index) => index > 1 && number !== arr[index -1] + 1)
有没有人可以帮我分解上面的功能?
我写了很长的代码来解决这个任务,有人给我发了一个更正,说我的代码可以简化。
我要做的第一件事就是添加大括号并将其分解为多行:
// This function takes an Array
const firstNonConsecutive = arr => {
// And tries to find an item
return arr.find((number, index) => {
return index > 1 && // Starting from index 2 (the first 2 are ignored)
number !== arr[index - 1] + 1; // Where the value is not (previous value + 1)
});
}
console.log(firstNonConsecutive([128, 6, 2, 8])); // 2 -> ??
console.log(firstNonConsecutive([5, 6, 2, 8])); // 2 -> correct
console.log(firstNonConsecutive([5, 6, 7, 8])); // undefined -> correct
然后,如果您不知道该方法,您可以在某些时候查找有关 Array.prototype.find()
. It takes a callback function which it will execute on every item until that callback returns true
(or any ) 的一些文档。如果是,它将 return 该值。否则,如果没有项目符合条件,它将 return undefined
.
这应该足以理解它的作用了吧?
奇怪的是它是从索引 2 (index > 1)
开始的。使用这个逻辑,这意味着永远不会检查第一个项目。也许那个条件应该是 index > 0
:
// This function takes an Array
const firstNonConsecutive = arr => {
// And tries to find an item
return arr.find((number, index) => {
return index > 0 && // Starting from index 1 (the first is ignored)
number !== arr[index - 1] + 1; // Where the value is not (previous value + 1)
});
}
console.log(firstNonConsecutive([128, 6, 2, 8])); // 6 -> correct
console.log(firstNonConsecutive([5, 6, 2, 8])); // 2 -> correct
console.log(firstNonConsecutive([5, 6, 7, 8])); // undefined -> correct
这里有一个更解构的方式来写这个:
function firstNonConsecutive(arr) {
// Find an item that passes the condition
return arr.find(passesCondition);
function passesCondition(number, index) {
return index > 0 && number !== arr[index - 1] + 1;
}
}
console.log(firstNonConsecutive([128, 6, 2, 8])); // 6 -> correct
console.log(firstNonConsecutive([5, 6, 2, 8])); // 2 -> correct
console.log(firstNonConsecutive([5, 6, 7, 8])); // undefined -> correct
const firstNonConsecutive = arr => arr.find((number, index) => index > 1 && number !== arr[index -1] + 1)
有没有人可以帮我分解上面的功能? 我写了很长的代码来解决这个任务,有人给我发了一个更正,说我的代码可以简化。
我要做的第一件事就是添加大括号并将其分解为多行:
// This function takes an Array
const firstNonConsecutive = arr => {
// And tries to find an item
return arr.find((number, index) => {
return index > 1 && // Starting from index 2 (the first 2 are ignored)
number !== arr[index - 1] + 1; // Where the value is not (previous value + 1)
});
}
console.log(firstNonConsecutive([128, 6, 2, 8])); // 2 -> ??
console.log(firstNonConsecutive([5, 6, 2, 8])); // 2 -> correct
console.log(firstNonConsecutive([5, 6, 7, 8])); // undefined -> correct
然后,如果您不知道该方法,您可以在某些时候查找有关 Array.prototype.find()
. It takes a callback function which it will execute on every item until that callback returns true
(or any undefined
.
这应该足以理解它的作用了吧?
奇怪的是它是从索引 2 (index > 1)
开始的。使用这个逻辑,这意味着永远不会检查第一个项目。也许那个条件应该是 index > 0
:
// This function takes an Array
const firstNonConsecutive = arr => {
// And tries to find an item
return arr.find((number, index) => {
return index > 0 && // Starting from index 1 (the first is ignored)
number !== arr[index - 1] + 1; // Where the value is not (previous value + 1)
});
}
console.log(firstNonConsecutive([128, 6, 2, 8])); // 6 -> correct
console.log(firstNonConsecutive([5, 6, 2, 8])); // 2 -> correct
console.log(firstNonConsecutive([5, 6, 7, 8])); // undefined -> correct
这里有一个更解构的方式来写这个:
function firstNonConsecutive(arr) {
// Find an item that passes the condition
return arr.find(passesCondition);
function passesCondition(number, index) {
return index > 0 && number !== arr[index - 1] + 1;
}
}
console.log(firstNonConsecutive([128, 6, 2, 8])); // 6 -> correct
console.log(firstNonConsecutive([5, 6, 2, 8])); // 2 -> correct
console.log(firstNonConsecutive([5, 6, 7, 8])); // undefined -> correct