I think what I want is pretty simple but I can't really find the correct solution.
I have this kind of array in Javascript :
[0, 38, 136, 202, 261, 399]
And I get a generated value from 0 to 600 on a button click. What I need is to find the nearest lower value in this array.
For example, if the generated value is 198, I want to get 136 as the result. If the generated value is 300, I want 261... If it's 589, I want 399 etc etc.
Until now, I have tried with this code :
var theArray = [ 1, 3, 8, 10, 13 ];
var goal = 7;
var closest = null;
$.each(theArray, function(){
if (closest == null || Math.abs(this - goal) < Math.abs(closest - goal)) {
closest = this;
}
});
alert(closest);
But it only returns the closest value... Now I need the to get only the closest smaller value for the given number... How can I improve my algorithm to fit my needs?
Thanks!
A Simple Solution is to use two nested loops. The outer loop starts from the second element, the inner loop goes to all elements on the left side of the element picked by the outer loop and stops as soon as it finds a smaller element.
Therefore, to find out the closest number we just return the index of the found minimum in the given array indexArr. indexOf(min) .
So if the array is like [2, 5, 6, 7, 8, 8, 9] and the target number is 4, then closest element is 5. We can solve this by traversing through the given array and keep track of absolute difference of current element with every element. Finally return the element that has minimum absolute difference.
Find the closest or nearest number with array formulaSelect a blank cell, and enter below formula, and press the Ctrl + Shift + Enter keys together. Note: In this array formula of {=INDEX(B3:B22,MATCH(MIN(ABS(B3:B22-E2)),ABS(B3:B22-E2),0))}, B3:B22 is the range that you want to find the specific value.
Given an array of integers, find the closest smaller element for every element. If there is no smaller element then print -1 Recommended: Please try your approach on {IDE} first, before moving on to the solution. A simple solution is to run two nested loops. We pick an outer element one by one.
Find closest number in array. Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples: A simple solution is to traverse through the given array and keep track of absolute difference of current element with every element.
It is much more difficult to find the closest smaller value when your list of numbers is random and not ordered, but it is possible. Let's explore how to do this. If you want to follow along with this tutorial, download the example spreadsheet. In our example below, we have a list of material lengths in column A in the range A2:A13.
A simple solution is to traverse through the given array and keep track of absolute difference of current element with every element. Finally return the element that has minimum absolution difference. An efficient solution is to use Binary Search .
Reverse the array and use find
let arr = [0, 38, 136, 202, 261, 399];
let val = 300;
let number = arr.reverse().find(e => e <= val);
console.log(number);
If you array is sorted, and small enough, a really simple mode to do what you want it's simplly iterate over the array until number > number-in-array
then return the number on the previous position.
function getClosestValue(myArray, myValue){
//optional
var i = 0;
while(myArray[++i] < myValue);
return myArray[--i];
}
Regards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With