
- 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
Get intersection between two ranges in JavaScript
Suppose, we have two arrays of numbers that represents two ranges like these −
const arr1 = [2, 5]; const arr2 = [4, 7];
We are required to write a JavaScript function that takes in two such arrays.
The function should then create a new array of range, that is the intersection of both the input ranges and return that range.
Therefore, the output for the above input should look like this −
const output = [4, 5];
Example
The code for this will be −
const arr1 = [2, 5]; const arr2 = [4, 7]; const findRangeIntersection = (arr1 = [], arr2 = []) => { const [el11, el12] = arr1; const [el21, el22] = arr2; const leftLimit = Math.max(el11, el21); const rightLimit = Math.min(el12, el22); return [leftLimit, rightLimit]; }; console.log(findRangeIntersection(arr1, arr2));
Output
And the output in the console will be −
[ 4, 5 ]
- Related Articles
- Intersection of two arrays JavaScript
- Get the intersection of two sets in Java
- How to get the intersection of two arrays in MongoDB?
- How to find intersection between two Numpy arrays?
- How to get the difference between two arrays in JavaScript?
- How to get substring between two similar characters in JavaScript?
- Python - Fetch columns between two Pandas DataFrames by Intersection
- JavaScript Program for Finding Intersection of Two Sorted Linked Lists
- JavaScript Program for Finding Intersection Point of Two Linked Lists
- Get the property of the difference between two objects in JavaScript
- How to Create an Array using Intersection of two Arrays in JavaScript?
- JavaScript Program for Count Primes in Ranges
- How to find the intersection between two or more lists in R?
- Intersection of two arrays in Java
- Intersection of two arrays in C#

Advertisements