Logic for checking in-between two numbers
I woul开发者_如何学编程d like to determine where the zone is given an arbitrary number.
zones = [0, 150, 300, 400, 600, 800]
function checkZone(mouseX) {
// if mouseX is 321, it should be index 2 of zones array
}
You can use the following loop to do it. I've included an entire page for testing purposes.
<html>
<body>
<script type="text/javascript">
var i; var y = 0; var val = 321; var zones = [0,150,300,400,600,800];
for (i = 0; i < zones.length; i++)
if (val >= zones[i])
y = i;
document.write("Value " + val + " is at position " + y);
</script>
</body>
</html>
Using various test data:
Value -99 is at position 0
Value 0 is at position 0
Value 149 is at position 0
Value 150 is at position 1
Value 321 is at position 2
Value 521 is at position 3
Value 799 is at position 4
Value 800 is at position 5
Value 999 is at position 5
If your array is very large, checking each element will be slow. A binary search might be good in that case. Here's some example code (untested, but as well thought-out as I can do at this time of night):
function checkZone(mouseX) {
var low = 0, high = zones.length, i;
if (mouseX >= zones[high]) {
low = high
}
while (high - low > 1) {
i = low + Math.floor((high - low) / 2);
if (mouseX >= zones[i]) {
low = i;
} else {
high = i;
}
}
return zones[low];
}
Note: I tried posting this about 15 minutes ago but SO ate my answer and I had to redo most of it.
Is the array of numbers sorted in ascending order? If it is, then you have two options. If the size of array is relatively small, just iterate through it with a cycle, and find the appropriate zone. Otherwise implement a binary search (much faster).
This should be better and faster:
console.clear();
var zones = [800, 400, 150, 0, 300, 600]; // unsorted array
zones.sort(); // sort for function comparison
console.log(zones);
function checkZone(mouseX) {
for( var i = (zones.length-1); mouseX < zones[i--];){ }
return ++i;
}
// perform 25 tests with random mouseX values 0-1000
for (var rnd=1,runs=25; runs-- && (rnd=Math.floor( Math.random()*1000 ))>=0; ){
console.log( (25-runs) + ". "
+ "mouseX:" + rnd + " , "
+ "index:" + checkZone(rnd) // checkZone(number) is all that's really needed
);
}
The for-loop in the function searches the array from the last array value to the beginning. Once the param is no longer less than the array value, the loop quits and the function returns that element. We have to add one to the return variable because the loop doesn't add 1 back when the loop condition fails.
If you don't feel comfortable with console.log
, you can replace it with document.write
or alert
. When finished testing, all that's needed is the sorted array, the function, and the function call. You can scrap the testing loop and all the random number jibber-jabber.
Example output:
[0, 150, 300, 400, 600, 800]
1. mouseX:458 , index:3
2. mouseX:17 , index:0
3. mouseX:377 , index:2
4. mouseX:253 , index:1
5. mouseX:446 , index:3
6. mouseX:764 , index:4
7. mouseX:619 , index:4
8. mouseX:653 , index:4
9. mouseX:337 , index:2
10. mouseX:396 , index:2
11. mouseX:107 , index:0
12. mouseX:820 , index:5
13. mouseX:850 , index:5
14. mouseX:117 , index:0
15. mouseX:659 , index:4
16. mouseX:393 , index:2
17. mouseX:906 , index:5
18. mouseX:128 , index:0
19. mouseX:435 , index:3
20. mouseX:712 , index:4
21. mouseX:841 , index:5
22. mouseX:259 , index:1
23. mouseX:447 , index:3
24. mouseX:809 , index:5
25. mouseX:892 , index:5
精彩评论