forked from omiras/solved-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
21 lines (17 loc) · 855 Bytes
/
test.js
File metadata and controls
21 lines (17 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function firstNonConsecutive(arr) {
let i = 1; //Inicio de la variable del contador
let result = null;
while (i < arr.length && result == null) {
// mentre no hagi arribar al fi de l'array I no hagi trobat un número no consecutiu, tinc feina
//Recorremos el array de números mientras se cumpla la condición
if (arr[i] - arr[i - 1] !== 1) {
//Comprobamos si el número actual y el número anterior no se incrementan en 1
result = arr[i]; //Si se cumple, mostramos el número
}
i++;
}
return result; //Si al recorrer todo el array, se cumple mostramos null
}
console.log(firstNonConsecutive([1, 2, 3, 4, 6, 7, 8])); // 6
// console.log(firstNonConsecutive([1, 2, 3, 4])); // null (porque todos son consecutivos)
// console.log(firstNonConsecutive([7, 8, 10])); // 10