4

i'll be getting two values bigValue & smallValue based on this I should set maxValue & minValue, below are the conditions explained

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 & smallValue can be of any values & both will be positive.

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

19
  • Can there be any number of combinations? Could bigValue 42 come with smallValue 03, for example?
    – Matthijs
    Commented Feb 23, 2016 at 12:40
  • What's wrong with the logic you have? (perhaps except for missing else?). You probably could put those value in a table and do some sort of a composite key lookup, but I'm not sure whether it's worth it in your case. Commented Feb 23, 2016 at 12:40
  • @Matthijs yea that is possible
    – SKADIN
    Commented Feb 23, 2016 at 12:41
  • What is your question exactly? You don't know how to write this in javascript or different? Commented Feb 23, 2016 at 12:41
  • What is the logic behind this? It makes no sense
    – StudioTime
    Commented Feb 23, 2016 at 12:42

4 Answers 4

8

This proposal works with the logarithm of 10 for the wanted range. It works for small numbers as well, even with one min/max 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);

7
  • For value min=80 & max = 100, it is coming as left=0 & right=100.I would require it as left=80 & right=100.
    – SKADIN
    Commented Feb 24, 2016 at 9:55
  • 1
    that is then an exeption, because in your original question you are rounding by the most left digit, here its 080 and 100. the first value get floored to 000 and the second gets ceiled and stays at 100.
    – qqq
    Commented Feb 24, 2016 at 10:04
  • You can use console.log in your answers instead of document.write btw, it would show the same formatted output
    – vsync
    Commented Jul 23, 2016 at 18:44
  • @vsync, now it does, but at this time, i wrote the answer, console meant console and i work without console (mostly).
    – qqq
    Commented Jul 23, 2016 at 18:46
  • fails at { "min": 0, "max": 0 }
    – Redu
    Commented Sep 3, 2016 at 20:49
3

If, according to the example cases, your requirement is to set the bigValue to the nearest upper tenth and the smallValue to the nearest lower tenth: the following should work fine for both positive integers and positive decimals.

Using the ternary operator:

//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); 
4
  • If that's indeed what the OP wanted, because his code doesn't do exactly that. Commented Feb 23, 2016 at 12:53
  • @BartekBanachewicz: OP has specified in his comments and question that bigValue and smallValue can be any positive values.
    – dshgna
    Commented Feb 23, 2016 at 13:22
  • My bad!!I missed one more condition,please check.It is failing for fourth.
    – SKADIN
    Commented Feb 23, 2016 at 13:35
  • @SKADIN I've updated the answer for it to work for any number of decimal places.
    – dshgna
    Commented Feb 23, 2016 at 14:07
-1

This should be your answer :

Try changing the values of the function call parameters

EDIT : Snippet updated 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>

-3

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;
}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.