Reputation: 27
var value1=50
var value2=140
group 1
a,b a>=42 a<=126 , b>=140 b<=213
a,c a>=42 a<=126 , c>=1.40 c<=2.13
b,a b>=140 b<=213 , a>=42 a<=126
c,a c>=1.40 c<=2.13 , a>=42 a<=126
group 2
d,e d>=91 d<=443 , e>=58 e<=84
d,f d>=91 d<=443 , f>=4.8 f<=7
e,d e>=58 e<=84 , d>=91 d<=443
f,d f>=4.8 f<=7 , d>=91 d<=443
Need to program this into js so that the two values will check through the ranges of numbers for a suitable number range. Im looking to have it scan through the first group if not successfully finding two ranges to suit both values it checks with the second group.
I have tried a number of different methods for this but i cant work out how to do it?
Help much appreciated
Upvotes: 1
Views: 760
Reputation: 107566
I would start by setting up a simple map with the values that will be used in the checking. Keep in mind that this code is entirely written off the top of my head and not tested, but it might give you a starting point.
It appears that the a, b, etc. values are static. So you could set up a map to help you out:
var groupOneRanges =
{
a: { min: 42, max: 126 },
b: { min: 140, max: 213 },
c: { min: 1.4, max: 2.13 }
};
var groupTwoRanges =
{
d: { min: 91, max: 443 },
e: { min: 58, max: 84 },
f: { min: 4.8, max: 7 }
};
Then you can automatically set up the permutations of the ranges in each group with a little looping and an array:
var groupOnePairs = [];
for (var p1 in groupOneRanges) {
for (var p2 in groupOneRanges) {
if (p1 !== p2) {
groupOnePairs.push({ v1: groupOneRanges[p1], v2: groupOneRanges[p2]});
}
}
}
You can reuse that code to create groupTwoPairs
, but I'm leaving it out as an exercise for you.
Once you have the range combinations, you just have to do the comparisons:
var value1 = 50;
var value2 = 154;
var found = false;
// check group 1 first
for (var i = 0, c = groupOnePairs.length; i < c; i++) {
if (value1 >= groupOnePairs[i].v1.min && value1 <= groupOnePairs[i].v1.max) {
if (value2 >= groupOnePairs[i].v2.min && value2 <= groupOnePairs[i].v2.max) {
// found a suitable range!
found = true;
break;
}
}
}
if (!found) {
// check group 2 for each of the values as done above
}
There is quite a bit of room for improvement, but this is the plainest JS I could come up with.
Upvotes: 1
Reputation: 9180
Something like the following: http://jsbin.com/ipefuf/1/
It's a little verbose - could be shortened, but does roughly what you need. Start with it.
(function(){
var value1=50;
var value2=140;
var groups = [
{
name: "group 1",
ranges: [
{ name: "a,b", func: function(a,b){ return a>=42 && a<=126 && b>=140 && b<=213; } },
{ name: "a,c", func: function(a,c){ return a>=42 && a<=126 && c>=1.40 && c<=2.13; } },
{ name: "b,a", func: function(b,a){ return b>=140 && b<=213 && a>=42 && a<=126; } },
{ name: "c,a", func: function(c,a){ return c>=1.40 && c<=2.13 && a>=42 && a<=126; } }
]
},
{
name: "group 2",
ranges: [
{ name: "d,e", func: function(d,e){ return d>=91 && d<=443 && e>=58 && e<=84; } },
{ name: "d,f", func: function(d,f){ return d>=91 && d<=443 && f>=4.8 && f<=7; } },
{ name: "e,d", func: function(e,d){ return e>=58 && e<=84 && d>=91 && d<=443; } },
{ name: "f,d", func: function(f,d){ return f>=4.8 && f<=7 && d>=91 && d<=443; } }
]
}
];
var inRange = false;
for(var groupIndex in groups)
{
var group = groups[groupIndex];
for(var rangeIndex in group.ranges)
{
var range = group.ranges[rangeIndex];
inRange = range.func(value1, value2);
if(inRange)
{
alert("Group: " + group.name + ", Range: " + range.name);
break;
}
}
if(inRange)
{
break;
}
}
if(!inRange)
{
alert("Not in range.");
}
})();
Upvotes: 1