I have a large array of features with p and u properties. I want to find the smallest and highest p and u in the array and create this switch statement in a loop. This works about 99.9% of the time. However. I have one data set where the max and min turn out to be the same even if the values are evenly distributed and the average is not the same. Stumped.
switch(true) {
case p > max_p:
max_p = p;
case u > max_u:
max_u = u;
case p < min_p:
min_p = p;
case u < min_u:
min_u = u;
}
I run through the loop in firebug and can see that max_u gets sometimes updated if u < max_u. For example u = 0.066, max_u = 0.088.
Pycharm tells me about a fallthrough issue but the statement works fine on every other dataset I throw at it.
I can split the statement into two. The performance loss is minor but I would like to understand how this could happen.
Thanks, Dennis
edit:
Split into two statement that dataset works completely fine without a break in the statement.
switch(true) {
case p > max_p:
max_p = p;
case p < min_p:
min_p = p;
}
switch(true) {
case u > max_u:
max_u = u;
case u < min_u:
min_u = u;
}
edit: I accepted the answer given which works but I am still puzzled why something like this would happen.
Assuming you have an array of objects with
u
andp
properties (if I've read the question correctly), here's a simple function that will give you the min/max values you want and save you the problems of usingswitch
and/orif
conditions.DEMO