

- 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
JavaScript to check consecutive numbers in array?
To check for consecutive numbers like 100, 101, 102, etc., use the concept of reduce(). TRUE would be returned for consecutive numbers, else false is the return value.
Example
const sequceIsConsecutive = (obj) => Boolean(obj.reduce((output, lastest) => (output ? (Number(output.number) + 1=== Number(lastest.number) ? lastest : false) : false))); console.log("Is Consecutive="+sequceIsConsecutive ([{ number: '100' },{number: '101'} ,{number: '102' }])); console.log("Is Consecutive="+sequceIsConsecutive([{ number: '100' }, {number: '102'} ,{number: '104' }]));
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo126.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo126.js Is Consecutive=true Is Consecutive=false
- Related Questions & Answers
- Check three consecutive numbers - JavaScript
- Three strictly increasing numbers (consecutive or non-consecutive). in an array in JavaScript
- N consecutive odd numbers JavaScript
- Sum of consecutive numbers in JavaScript
- Check if list contains consecutive numbers in Python
- Finding three desired consecutive numbers in JavaScript
- Count of pairs in an array that have consecutive numbers using JavaScript
- Check if three consecutive elements in an array is identical in JavaScript
- Consecutive elements sum array in JavaScript
- Compress array to group consecutive elements JavaScript
- Check if array elements are consecutive in Python
- Maximum consecutive numbers present in an array in C++
- Check if items in an array are consecutive but WITHOUT SORTING in JavaScript
- Can array form consecutive sequence - JavaScript
- Find the longest sub array of consecutive numbers with a while loop in JavaScript
Advertisements