假设我有像
这样的数字数组00
05
10
15
upto
55
现在我想开发一个例程,我将传递数组和输入数字。 假设我将32作为输入传递,因此将搜索32 数组和返回35因为35是最接近最高的32,所以只需指导我如何像那个例行程一样发展。
这里我开发了一个返回30最接近32但我想要35应该返回。我知道常规中存在一些缺陷。如果有人帮我构建正确的例行程序,那会有所帮助。
这是我的,没有返回预期的结果。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
var array = [00, 05, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55];
var number = 32;
alert (closest (number, array)); // display output here
function closest (num, arr) {
var curr = arr[0];
var diff = Math.abs (num - curr);
for (var val = 0; val < arr.length; val++) {
var newdiff = Math.abs (num - arr[val]);
if (newdiff < diff) {
diff = newdiff;
curr = arr[val];
}
}
curr=(curr>=10)?curr:'0'+curr; //If result is 5, to display '05' instead of '5'
return curr;
}
});
</script>
感谢
$(document).ready(function () {
var array = [00, 05, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55];
test(32)
test(4)
test(100)
test(47)
test(45)
function test(num) {
//log(num + ' --> ' + closest(num, array)); // display output here
alert(closest(num, array));
}
function closest(num, arr) {
var curr = Infinity;
for (var i = 0; i < arr.length; i++) {
if (arr[i] >= num && curr > arr[i]) {
curr = arr[i]
}
}
curr = (curr >= 10) ? curr : '0' + curr; //If result is 5, to display '05' instead of '5'
return curr;
}
});
var log = (function () {
var $log = $('#log');
return function (msg) {
$('<p/>', {
text: msg
}).prependTo($log)
}
})();
答案 0 :(得分:2)
你可以做到
$(document).ready(function() {
var array = [00, 05, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55];
test(32)
test(4)
test(100)
test(47)
test(45)
function test(num) {
log(num + ' --> ' + closest(num, array)); // display output here
}
function closest(num, arr) {
var curr = Infinity;
for (var i = 0; i < arr.length; i++) {
if (arr[i] >= num && curr > arr[i]) {
curr = arr[i]
}
}
curr = (curr >= 10) ? curr : '0' + curr; //If result is 5, to display '05' instead of '5'
return curr;
}
});
var log = (function() {
var $log = $('#log');
return function(msg) {
$('<p/>', {
text: msg
}).prependTo($log)
}
})();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="log"></div>
&#13;
答案 1 :(得分:1)
快(O(n))
并且足够简单。
此方法获得精确且最接近的值。低,精确和高。
var max = function (arr) { return Math.max.apply(null, arr); };
var min = function (arr) { return Math.min.apply(null, arr); };
var nearest = function (arr, x) {
var l = [], h = [];
var exact = -1;
arr.forEach(function (v) {
if (v == x) exact = v;
((v < x) && l.push(v)) || ((v > x) && h.push(v));
});
return { "low": arr.indexOf(max(l)), "exact": exact, "high": arr.indexOf(min(h)) };
};
var array = [00, 05, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55];
var testValue = 30;
var range = nearest(array, testValue);
if (range.exact != -1) {
alert("Exact Value " + testValue)
}
alert("Near Low:" + array[range.low] + " Near High:" + array[range.high]);
答案 2 :(得分:1)
http://jsfiddle.net/59n6f5qv/1/
<强>更新强>
var array = [00, 05, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55];
var number = 32;
alert(closest(number, array)); // display output here
function closest(num, arr) {
var next = Math.max.apply(Math, arr);
for (var val = 0; val < arr.length; val++) {
if (arr[val] >= number && arr[val] < next) next = arr[val];
}
curr = next;
curr = (curr >= 10) ? curr : '0' + curr; //If result is 5, to display '05' instead of '5'
return curr;
}
答案 3 :(得分:0)
function closest(num, arr) {
if (arr.length == 0) { throw ("Array is empty"); }
if (arr[0] > actualnum) { return arr[0]; }
var diff = actualnum - arr[0];
for (var i = 1; i < arr.length; i++) {
if (actualnum <= arr[i]) {
return (arr[i] - actualnum > diff) ? arr[i - 1] : arr[i];
} else {
diff = actualnum - arr[i];
}
}
return arr[arr.length - 1];
}