2

I need to create a dynamic scales something like this

Range 1  = 0   to 100
Range 2  = 100 to 200
Range 3  = 200 to 300
Range 4  = 300 to 400
Range 5  = 400 to 500
Range 6  = 600 to 700
Range 7  = 700 to 800
Range 8  = 800 to 900
Range 9  = 900 to 1000

Here, ranges are 1 to 9 and minimum value is 0 and maximum value is 1000. These ranges, minimum and maximum values are dynamic.

So, I required a function to return the scales.

For example:-

    function getScales(minRage, maxRange, minValue, maxValue){
      var scales={};
       ..........
       ............
      return scales;
    }
   //Output:
   [
    {
        range   :1
        min     :0,
        max     :100
    },
    {
        range   :2
        min     :100,
        max     :200
    },
    {
        range   :3
        min     :200,
        max     :300
    },
    ....
    ....
    {
        range   :9,
        min     :900,
        max     :1000
    }
  ]

To get above result , I need to call the function like this getScales(1, 9, 0, 1000).

This is what my actual requirement: if I call getScales(1, 5, 4000, 418500);

2
  • 2
    "Please help me create a function" is not a good question. It should be more like "Please help me fix my function, I tried this and it is not working because [explain]" Commented Feb 17, 2014 at 8:10
  • If you don't know the answer... don't put such a comment like this. Now, you can copy the below answer if you want ... :) Commented Feb 17, 2014 at 11:15

2 Answers 2

9

Have a look at this:

function getScales(minRange, maxRange, min, max){
    var scales = [],                  // Prepare some variables
    ranges = maxRange+1 - minRange,   // Amount of elements to be returned.
    range  = (max-min)/ranges;        // Difference between min and max
    for(var i = 0; i < ranges; i++){
        scales.push({
            range: i+minRange,        // Current range number
            min: min + range * i,
            max: min + range * (i+1)
        });
    }
    return scales;
}

You can call the function like this:

getScales(0, 9, 0, 1000);

Output:

[
    {
        "range": 0,
        "min": 0,
        "max": 100
    },
    {
        "range": 1,
        "min": 100,
        "max": 200
    },
    .......
    {
        "range": 8,
        "min": 800,
        "max": 900
    },
    {
        "range": 9,
        "min": 900,
        "max": 1000
    }
]

To get rid of the floating point errors in the output, you can replace:

min: range * i,
max: range * (i+1)

With:

min: (range * i).toFixed(2),
max: (range * (i+1)).toFixed(2)

Replace the 2 with the desired amount of digits behind the decimal point.

Sign up to request clarification or add additional context in comments.

4 Comments

Could the person that downvoted me please explain why? If I made a mistake, I'd like to learn from it.
cerbus,thanks for post a answer but please check this getScales(0, 4, 253, 467). Output: [{"range":0,"min":**0**,"max":42.8},{"range":1,"min":42.8,"max":85.6},{"range":2,"min":85.6,"max":128.39999999999998},{"range":3,"min":128.39999999999998,"max":171.2},{"range":4,"min":171.2,"max":**214**}] this is wrong.The first object should like this {"range":0,"min":**253**,"max":42.8} and final object should like this {"range":4,"min":171.2,"max":**467**}.
Hm, @Mohammedshafeek: That looks like some JavaScript float precision errors. (Basically, a flaw in the way JavaScript handles floats). What kind of precision do you require in your output? Only one digit behind the decimal point?
Consider adding the passed 'min' value as an offset to the output min and max.
0

Something like that:

var factory = function(start, end, minV, maxV){
    var result = [],
        st = (maxV - minV) / (end - start + 1),
        buffer = minV;
    for(var i = 0; i <= end - start; i++){
        buffer = st * i + minV;
        result.push({
            range: i,
            minValue: buffer.toFixed(2),
            maxValue: (buffer + st).toFixed(2)
        });
    }   
    return result;
}

console.log(JSON.stringify(factory(0, 4, 253, 467)))

Some explanation: @start and @end describe number of ranges, @minV - first range starts with this value, @maxV - last range ends with this

Output

[{"range":0,"minValue":"253.00","maxValue":"295.80"},{"range":1,"minValue":"295.80","maxValue":"338.60"},{"range":2,"minValue":"338.60","maxValue":"381.40"},{"range":3,"minValue":"381.40","maxValue":"424.20"},{"range":4,"minValue":"424.20","maxValue":"467.00"}] 

Play with demo

12 Comments

This doesn't work very well if the min range starts at something that isn't 0, like: factory(5, 9, 0, 1000)
Of course i can start ranges with 0, but it not so important)
If the OP specifies a min and a max range, you should assume both are variable. factory(5, 9, 0, 1000) returns an array with this: {"range":9,"minValue":1800,"maxValue":2000} as it's last value. minValue and maxValue both exceed the value of the maxV parameter. Sure, your code doesn't throw any errors, but that doesn't mean it works as intended.
@Cerbus i fixed this moment
I downvoted you because your code didn't work. The fix works, but there's no need to keep the old version around, now. That only leads to confusion. I'd appreciate it if you'd remove your downvote from my answer. (Unless there is actually something wrong with it.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.