如何检查数字是否在两个值之间？

2020/11/12 18:42 · javascript ·  · 0评论

if (windowsize > 500) {
// do this
}

if (windowsize > 500-600) {
// do this
}

if (windowsize > 500 && windowsize < 600) {
// ...
}

Number.prototype.between = function(a, b) {
var min = Math.min.apply(Math, [a, b]),
max = Math.max.apply(Math, [a, b]);
return this > min && this < max;
};

var windowSize = 550;

console.log(windowSize.between(500, 600));

Number.prototype.between = function(a, b, inclusive) {
var min = Math.min.apply(Math, [a, b]),
max = Math.max.apply(Math, [a, b]);
return inclusive ? this >= min && this <= max : this > min && this < max;
};

var windowSize = 500;

console.log(windowSize.between(500, 603, true));

……Function.prototype.apply()慢！除了当您有固定数量的参数时调用它，是没有意义的……

Number.prototype.between = function(a, b) {
var min = Math.min(a, b),
max = Math.max(a, b);

return this > min && this < max;
};

var windowSize = 550;

console.log(windowSize.between(500, 600));

Number.prototype.between = function(a, b, inclusive) {
var min = Math.min(a, b),
max = Math.max(a, b);

return inclusive ? this >= min && this <= max : this > min && this < max;
}

var windowSize = 500;

console.log(windowSize.between(500, 603, true));

if (500 < size && size < 600) { doStuff(); }

lodash具有_.inRange()功能https://lodash.com/docs/4.17.4#inRange

_.inRange(3, 2, 4);
// => true

if (windowsize > 500-600) {
// do this
}

windowSize > -100

&&例如，您应该使用严格检查这两种情况，如下所示

if( windowSize > 500 && windowSize < 600 ){

// Then doo something

}

jQuery.noConflict();
var Ammount = document.getElementById('Ammount');

if (\$(Ammount).val() >= 250 && \$(Ammount).val() <= 499) {
{
\$('#myModal').modal();
\$("#myModalLabelbronze").show();
\$("#myModalLabelsilver").hide();
\$("#myModalLabelgold").hide();
\$("#myModalPbronze").show();
\$("#myModalPSilver").hide();
\$("#myModalPGold").hide();
}
}
});

if (Math.abs(v-550)<50) console.log('short')
if ((v-500)*(v-600)<0) console.log('short')

if (Math.abs(v-max+v-min)<max+min) console.log('short')
if ((v-min)*(v-max)<0) console.log('short')