
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding the continuity of two arrays in JavaScript
We are required to write a JavaScript function that takes in two arrays of numbers. And the function should return true if the two arrays upon combining and shuffling can form a consecutive sequence, false otherwise.
For example: If the arrays are −
const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7];
Then the output should be true.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7]; const canFormSequence = (arr1, arr2) => { const combined = [...arr1, ...arr2]; if(combined.length < 2){ return true; }; combined.sort((a, b) => a-b); const commonDifference = combined[0] - combined[1]; for(let i = 1; i < combined.length-1; i++){ if(combined[i] - combined[i+1] === commonDifference){ continue; }; return false; }; return true; }; console.log(canFormSequence(arr1, arr2));
Output
The output in the console will be −
true
- Related Articles
- Finding the difference between two arrays - JavaScript
- Finding the common streak in two arrays in JavaScript
- Finding deviations in two Number arrays in JavaScript
- Finding the missing number between two arrays of literals in JavaScript
- Finding maximum number from two arrays in JavaScript
- Finding the inclination of arrays in JavaScript
- Finding the intersection of arrays of strings - JavaScript
- Finding intersection of multiple arrays - JavaScript
- Finding intersection of arrays of intervals in JavaScript
- Finding reversed index of elements in arrays - JavaScript
- Finding content of arrays on the basis of specific property in JavaScript?
- Finding Common Item Between Arbitrary Number of Arrays in JavaScript
- Finding intersection of arrays that contain repetitive entries in JavaScript
- isSubset of two arrays in JavaScript
- Finding the sum of all common elements within arrays using JavaScript

Advertisements