JSFiddle - React, Tailwind, and code Playground
by phrontisteries
HTML
<html><body><div id="output"></div></body></html>
JavaScript
function typeis(x){var ty=typeof x!=="object"?typeof x:Object.prototype.toString.call(x).split(' ')[1].slice(0,-1).toLowerCase();return ty.slice(0,4)!=="html"?ty:x.tagName.toLowerCase()}
var n = 0;
function out(v){var d=document,o=d.getElementById("output");o.appendChild(d.createTextNode((++n)+": "+v));o.appendChild(d.createElement("br"))}
function valBetween(v, min, max, r){
var a = typeis(v)==='array',
mn = typeof(min)==="boolean"||typeof(min)==="undefined"||typeof(min)==="null",
mx = typeof(max)==="boolean"||typeof(max)==="undefined"||typeof(max)==="null";
min = typeof(min)==='number'?min:Number.MIN_VALUE;
max = typeof(max)==='number'?max:Number.MAX_VALUE;
if(mn){ // no min // 1 - 4
if(mx){ // no max // 1 , 2, 4
r = v
}else{ // MAX // 3
r=a?Math.max.apply(Math,v.filter(function(x){return x<max})):Math.max.apply(Math,v<max?v:max)
}
}else{ // MIN // 5 - 7
if(mx){ // no maximum // 5 , 7
r=a?Math.min.apply(Math,v.filter(function(x){return x>min})):Math.min.apply(Math,v>min?v:min)
}else{ // MIN & MAX // 6
// but do we want the Min or Max of those Values?
if(a){ // or just return values between?
v=v.filter(function(x){return ((x<max)&&(x>min))});
//r=Math.max.apply(Math,v);
//r=Math.min.apply(Math,v);
}else{
r=Math.max.apply(Math,v<max?v:max);
//r=Math.min.apply(Math,v>min?v:min);
}
}
}
out(r?r:v)
}
var a = [7.74, 0, 99.92, 0, 99.53, 55.5, 97.15, 85.16, 67.4, 0, 77.22, 0, 57.74, 0, 53.95, 0, 86.68, 0, 86.66, 0, 98.5, 0, 29.1, 0, 98.35, 0, 100, 0, 99.04, 96.96];
valBetween(a);//1
valBetween(a, false);//2
valBetween(a, false, 90);//3
valBetween(a, false, false);//4
valBetween(a, -1);//5
valBetween(a, 60, 70);//6
valBetween(a, 70, 80);//6
valBetween(a, 60, false);//7