3
\$\begingroup\$

I need to generate a minimum and maximum value for a range of speeds (slow, medium, fast), the user can specify any combination of the 3 values, and receive a range that encompasses all.

Given any combination of "slow", "medium", "fast", getRange() will return a [min, max] range.

The function can be called in the following ways:

// Any combination of the values
getRange(['slow', 'medium', 'fast']); // [0, 100]
getRange(['medium', 'fast']); // [36, 100]
getRange(['slow']); // [0, 35]

// Or just a single string
getRange('fast'); // [76, 100]

// No string specified
getRange(); // [0, 100]

Is there any way to simplify my code? It seems a bit clunky and there's probably a simpler way of writing this:

function getRange(speed){
    var min, max;

    if(speed && speed.length){
        var obj = {};

        if(Array.isArray(speed)){
            speed.forEach(function(s){
                obj[s] = true;
            });
        }
        else{
            obj[speed] = true;
        }

        if(obj.slow){
            min = 0;
            max = 35;
        }

        if(obj.medium){
            if(typeof min == 'undefined'){
                min = 36;
            }
            max = 75;
        }

        if(obj.fast){
            if(typeof min == 'undefined'){
                min = 76;
            }
            max = 100;
        }
    }

    if(typeof min == 'undefined'){
        min = 0;
    }

    if(typeof max == 'undefined'){
        max = 100;
    }

    return [min, max];
};
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

I do believe this could be done easier. You could use the || shortcut to set min and max if you could not find a speed range, and you could extract the numbers into a data structure and go from there.

Something like this:

function getRange(speed){
    //Speed range config, in array to preserve order
    var speedRanges = [
            { name: 'slow',   min: 1  , max : 35 },
            { name: 'medium', min: 36 , max : 75 },
            { name: 'fast',   min: 76 , max : 100 }
        ],
        min, max, i, range;
    //Build logic once for arrays, if we are not dealing with an array,
    //then turn speed into an array
    if (!Array.isArray(speed))
        speed = [speed];
    //Check for each speed range, apply if applicable 
    for (i = 0; i < speedRanges.length; i++) {
        range = speedRanges[i];
        if (~speed.indexOf(range.name)) {
            min = min || range.min;
            max = range.max;
        }
    }

    min = min || 1;
    max = max || 100;
    return [min, max];
}

If you absolutely need slow to be 0 to 35 then you will have to enhance min = min || range.min; since 0 counts as not set..

\$\endgroup\$
1
  • \$\begingroup\$ Thanks, min = 0 is not really important. \$\endgroup\$
    – Drahcir
    Apr 29, 2014 at 13:39

Your Answer

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

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