A JavaScript implementation of a data finder function that uses closures to search for values within specified ranges of an array.
Implement the function dataFinder such that:
- It takes a single argument
data, an array of integers - It returns a new function called
find - The
findfunction takes 3 arguments:minRange,maxRange, andvalue - It searches for the
valuein thedataarray within the inclusive range[minRange, maxRange]using 0-based indexing - Returns
trueif the value is found in the given range,falseotherwise - Throws an
Errorwith message 'Invalid range' ifminRangeormaxRangeis beyond the bounds of the array
- Closure Pattern: The returned
findfunction maintains access to the originaldataarray - Range Validation: Validates that indices are within array bounds
- Efficient Search: Linear search within the specified range
- Error Handling: Proper error handling for invalid ranges
const finder = dataFinder([1, 6, 3, 0, 2, 15, 10]);
// Search for value 10 in range [2, 4] (indices 2 through 4)
const result = finder(2, 4, 10); // Returns false
// Subarray searched: [3, 0, 2] - 10 not foundconst finder = dataFinder([15, 1, 10, 5, 4, 20]);
// Search for value 4 in range [1, 4]
const result = finder(1, 4, 4); // Returns true
// Subarray searched: [1, 10, 5, 4] - 4 found at index 4const finder = dataFinder([10, 1, 0, 13, 4, 15]);
try {
const result = finder(1, 10, 13);
} catch (error) {
console.log(error.message); // "Invalid range"
// maxRange (10) is beyond array length (6)
}node dataFinder.js < input.txtLine 1: Space-separated integers representing the data array
Line 2: Three space-separated integers: minRange maxRange value
15 1 10 5 4 20
1 4 4
true
Input:
15 1 10 5 4 20
1 4 4
Output:
true
Explanation: Searching for 4 in range [1, 4] of array [15, 1, 10, 5, 4, 20]. Value 4 is found at index 4.
Input:
1 6 3 0 2 15 10
2 4 10
Output:
false
Explanation: Searching for 10 in range [2, 4] of array [1, 6, 3, 0, 2, 15, 10]. The subarray [3, 0, 2] does not contain 10.
Input:
10 1 0 13 4 15
1 10 13
Output:
Error: Invalid range
Explanation: maxRange (10) exceeds the array bounds (length 6).
- Maximum array length: 10
- All array elements are integers
- 0-based indexing is used
The dataFinder function demonstrates:
- Closures: The inner
findfunction has access to thedataparameter - Validation Logic:
- Checks if
minRange < 0 - Checks if
maxRange >= data.length - Checks if
minRange > maxRange
- Checks if
- Search Algorithm: Simple linear search through the specified range
- Error Handling: Throws Error object with specific message for invalid ranges
MIT License - Feel free to use this code for learning and development purposes.
Created as a solution to a JavaScript coding challenge demonstrating closure patterns and array manipulation.