0

I am using the following code to find the closest value in an array.

var x=[0,1,2,3,4,5];
var pointX=1.5;

$.each(x, function() {
    if (closest == null || Math.abs(this - pointX) < Math.abs(closest - pointX)) {
        closest = this;
    }
});

This will return 2.

Now consider the following scenario:

var x=[20,21,22,23,24,25]

var pointX=1.5

For this case it returns 20, but I don't want it to because 1.5 is not in the range (20-25). It should instead return null in this case. How can I do this?

9
  • "it will return 20. but it's wrong. it will return null." - Do you mean that you want it to return 20? Or you want it to return null? Commented May 9, 2013 at 9:54
  • I'm confused by the last part of the question. Are you saying it should return 20 but doesn't, and instead returns null? Or should it return null (because it's not within the min and max value) but is instead returning 20? Commented May 9, 2013 at 9:54
  • no. it will return 20. but i don't want this. i need nothing will be returned (i.e. null) Commented May 9, 2013 at 9:55
  • This has absolutely nothing to do with JQuery. I'm removing the tag. Commented May 9, 2013 at 9:57
  • Are the values in the array always going to be in order like your two examples? Commented May 9, 2013 at 9:59

2 Answers 2

3

Try

function closest(x, pointX){
    var closest = null;

    if(pointX < x[0] || pointX > x[x.length -1]){
        return null
    }


    $.each(x, function(i,v) {
        if (closest == null || Math.abs(v - pointX) <= Math.abs(closest - pointX)) {
            closest = v;
        }
    });
    return closest;
}

Demo: Fiddle

Another way is to sort the array to make sure the indexes are proper

function closest(x, pointX){
    var closest = null, array = x.slice();

    array.sort();

    if(pointX < array[0] || pointX > array[array.length -1]){
        return null
    }

    $.each(array, function(i,v) {
        if (closest == null || Math.abs(v - pointX) <= Math.abs(closest - pointX)) {
            closest = v;
        }
    });
    return closest;
}
Sign up to request clarification or add additional context in comments.

3 Comments

good answer, but only if the input array is sorted lowest to highest
you assume the array is sorted
@SteveClaridge that was an assumption
1

If the input array will always be sorted then Arun's answer is fine. Otherwise this should work for you:

function getClosest(x, pointX) {
    var closest = null;

    // Work out min and max
    var min = Math.min.apply(null, x);
    var max = Math.max.apply(null, x);

    // Only calculate closest if point is within array
    if (pointX >= min && pointX <= max) {    
        var i;
        // removed the each as it wasn't really necessary 
        for (i = 0; i < x.length; i++) {                         
            if (closest == null || Math.abs(x[i] - pointX) < Math.abs(closest - pointX)) {
                closest = x[i];
            }
        }
    }
    return closest;
}

Example - http://jsfiddle.net/ZLd2S/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.