How to know the nearest number of another one in javascript


How to know the nearest number of another one in javascript



I wanted to know how can I know the nearest number of another one in javascript.



For example:



If we get like 3 numbers, 1, 2 and 5.



How could I know the nearest number to 4, which would be 5.



Cheers and Thanks in Advance.





We're not going to just write the code for you. What have you done to try and solve the problem and where did you get stuck? Please edit your question to include your most recent attempt.
– Mike C
May 16 '17 at 21:04






What if there's a tie?
– j08691
May 16 '17 at 21:05




4 Answers
4



You can loop throught that list with a for loop


for loop



I will give you an algorithm--maybe it will help you along.


Loop through your comparison numbers (e.g. 1, 2, and 5).

On each loop iteration: {

Calculate the delta between the comparison number and your
target number (e.g. 5). The delta is the absolute value
from subtracting one number from the other. E.g. delta of 4 and 5 is 1.

If the delta is the smallest so far, store that delta and
the value it corresponds to in tracking variables. Or if no
values have yet been assigned to those tracking variables,
also store the delta and corresponding value.

}

After loop exit, return the "corresponding value" from your tracking variables.



If you have follow up questions, try to give some code to show a specific problem you're having difficulty with.



Let's say you have your number in an array called nums and your number you want to compare in a variable x


nums


x


var nums = [1, 2, 5];
var x = 4



You can do the following:


function findClosest (nums, x) {
var sortNums = nums.sort(function(a, b){return a-b});
var diff = ;
var x = nums.length;
for (i = 0; i < x; i++) {
diff[i] = Math.abs(sortNums[i] - n);
}
var sortDiff = diff.sort(function(a, b){return a-b});
var smallestDiff = sortDiff[0];
for (i = 0; i < x; i++) {
if (smallestDiff == Math.abs(nums[i] - n)) {
return nums[i];
}
}
}



And then you call the function:


findClosest (nums, x);



Note: if you have 2 numbers which are as close to x, e.g. if x=5 and you have 4 and 6 as the closest numbers, you will get whichever one is earliest in the array nums.


x=5


nums



Based on Erik Hermansen's algorithm, I have made this code. It will probably be more efficient than my previous answer, since it only has one loop and there is no sorting involved.



Again, in the case where there are 2 different numbers with the same difference, the one that appears first in the array will be returned.


var numbers = [1, 2, 5];
var terget = 4;

function findClosest (nums, x) {
n = nums.length;
var diff = -1;
var smallest;
var d;
var smallArray = ;
for (i = 0; i < n; i++) {
d = Math.abs(nums[i] - x);
if (diff < 0 || d < diff) {
diff = d;
smallest = nums[i];
}
}
return smallest;
}

findClosest (numbers, target);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined