
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding the first non-consecutive number in an array in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of numbers. Our function should return that first element from the array which is not the natural successor of its previous element.
It means we should return that element which is not +1 its previous element given that there exists at least one such element in the array.
Example
Following is the code −
const arr = [1, 2, 3, 4, 6, 7, 8]; const findFirstNonConsecutive = (arr = []) => { for(let i = 0; i < arr.length - 1; i++){ const el = arr[i]; const next = arr[i + 1]; if(next - el !== 1){ return next; }; }; return null; }; console.log(findFirstNonConsecutive(arr));
Output
Following is the console output −
6
- Related Questions & Answers
- JavaScript Find the first non-consecutive number in Array
- Finding the index of the first element that violates the series (first non-consecutive number) in JavaScript
- Finding the largest non-repeating number in an array in JavaScript
- Finding first non-repeating character JavaScript
- Finding the first redundant element in an array - JavaScript
- Three strictly increasing numbers (consecutive or non-consecutive). in an array in JavaScript
- Finding unlike number in an array - JavaScript
- JavaScript Finding the third maximum number in an array
- Finding the first non-repeating character of a string in JavaScript
- Finding confusing number within an array in JavaScript
- Finding the third maximum number within an array in JavaScript
- Finding the nth missing number from an array JavaScript
- Return the first duplicate number from an array in JavaScript
- Detecting the first non-repeating string in Array in JavaScript
- Detecting the first non-unique element in array in JavaScript
Advertisements