How to get Min and max values ​​based on given values?

I get two values ​​bigValue and smallValue based on this, I have to set maxValue and minValue, the conditions are explained below

if bigValue=42 & smallValue=23 then maxValue=50 & minValue=20

if bigValue=12 & smallValue=03 then maxValue=20 & minValue=0

if bigValue=0.156 & smallValue=0.1 then maxValue=0.2 & minValue=0.1

if bigValue=0.0156 & smallValue=0.01 then maxValue=0.02 & minValue=0.01

How to achieve this with better logic using only javascript?

Here bigValue and smallValue can have any value, and both will be positive.

I have this requirement to set the start and end point for the y axis in the graph.

+4
source share
4 answers

This sentence works with a logarithm of 10 for the required range. It also works for small numbers, even with one minimum and maximum zero value and negative values.

function getBorders(min, max) {
    var v = Math.pow(10, Math.floor(Math.log(Math.max(Math.abs(min), Math.abs(max))) / Math.log(10)));
    return {
        left: Math.floor(min / v) * v || 0,
        right: Math.ceil(max / v) * v || 0
    };
}

var values = [{ "min": 80, "max": 100 }, { "min": 23, "max": 42 }, { "min": 3, "max": 12 }, { "min": 0.1, "max": 0.156 }, { "min": 0.01, "max": 0.0156 }, { "min": 30, "max": 255 }, { "min": 1255, "max": 2784 }, { "min": 0.0023, "max": 0.00769 }, { "min": 0, "max": 0.002 }, { "min": 0, "max": 15000 }, { "min": -23, "max": 0 }, { "min": -123, "max": 2 }, { "min": 0, "max": 0 }];

values.forEach(function (a) {
    var o = getBorders(a.min, a.max);
    Object.keys(o).forEach(function (k) { a[k] = o[k]; });
});

console.log(values);
Run code
+8
source

, , , bigValue , smallValue - : , .

:

//if decimal: get the largest non-zero position
if (bigValue< 1) {
   decBig = Math.ceil(-Math.log10(bigValue));}

if (smallValue< 1) {
   decSmall = Math.ceil(-Math.log10(smallValue))};

maxValue =  bigValue >= 1? Math.ceil(bigValue/10) *10 : Math.ceil(bigValue*Math.pow(10,decBig))/Math.pow(10,decBig);
minValue =  smallValue>= 1? Math.floor(smallValue /10) *10 : Math.floor(smallValue*Math.pow(10,decBig))/Math.pow(10,decBig); 
+3

This should be your answer:

Try changing function call parameter values

EDIT : Updated snippet to satisfy your last condition

function myFun(bigValue, smallValue) {
  var min_val;
  var max_val;
  
  var min = Math.min(bigValue, smallValue);
  var max = Math.max(bigValue, smallValue);
  
  if(parseInt(bigValue)==0) {
       var i = 1;
       while(bigValue < 1){
          bigValue *= 10;
          i *= 10;
       }
       max_val = Math.ceil(bigValue)/i;
  }
  else {
       max_val = Math.ceil(max/10)*10;
  }
  if(parseInt(smallValue)==0) {
       var i = 1;
       while(smallValue < 1){
          smallValue *= 10;
          i *= 10;
       }
       min_val = Math.floor(smallValue)/i;
  }
  else {
       min_val = Math.floor(min/10)*10;
  }
 document.getElementById("demo").innerHTML = "Max value :" + max_val + ", Min value :" + min_val; 
}
<button onclick="myFun(0.156,0.1)">Decimal 1</button>
<button onclick="myFun(0.0156,0.01)">Decimal 2</button>
<button onclick="myFun(42,23)">Integer</button>
<p id="demo"></p>
Run code
-1
source

Is this what you are looking for?

if (bigValue===42 && smallValue===23){
maxValue=50 ;
 minValue=20;
}
else if(bigValue===12 && smallValue===03){
maxValue=20 ;
 minValue=0;
}
else if( bigValue===0.156 && smallValue===0.1){
maxValue=0.2 ;
 minValue=0.1;
}
-3
source

Source: https://habr.com/ru/post/1630063/


All Articles