gpt4 book ai didi

javascript - js双值范围检查

转载 作者:行者123 更新时间:2023-11-30 18:16:12 24 4
gpt4 key购买 nike

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

需要将其编程到js中,以便这两个值将检查数字范围以获得合适的数字范围。如果没有成功找到两个范围以适合它与第二组检查的两个值,我希望让它扫描第一组。

为此我尝试了多种不同的方法,但我不知道该怎么做?

非常感谢帮助

最佳答案

类似于以下内容:http://jsbin.com/ipefuf/1/

它有点冗长 - 可以缩短,但大致可以满足您的需要。从它开始。

(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.");
}

})();

关于javascript - js双值范围检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13184069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com