-6
\$\begingroup\$

I'm trying to get some information on how to improve this function in Javascript using ES6/ES7

function inclusive(min, max, input) {
  var difference = function(a, b) {
    return Math.abs(max - min)
  };
  return Array.apply(0, Array(difference() + 1))
    .map(function(element, index) {
      var integer = index + min;
      if (input.indexOf(integer) === -1) {
        return integer;
      }
    }).filter(function(n) {
      return typeof n !== 'undefined'
    });
}
\$\endgroup\$

closed as off-topic by IEatBagels, dfhwze, Sᴀᴍ Onᴇᴌᴀ, pacmaninbw, 1201ProgramAlarm Aug 22 at 22:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – IEatBagels, dfhwze, Sᴀᴍ Onᴇᴌᴀ, pacmaninbw, 1201ProgramAlarm
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    \$\begingroup\$ Please explain what "this function" does, and make that the title of your question. See How to Ask. \$\endgroup\$ – 200_success Oct 28 '16 at 21:07
  • \$\begingroup\$ you should be able to look at it and tell what it does, i'm simply looking for anyone's input on es6/es7 versions of the function \$\endgroup\$ – iamwhitebox Oct 28 '16 at 21:09
  • 6
    \$\begingroup\$ That's not the point. It's your job to present reviewers your code, tell us about it. And imagine the front page if every single question was "How could I improve this code?" - we have established community consensus on posting guidelines through the years for a reason. \$\endgroup\$ – Mathieu Guindon Oct 28 '16 at 21:20
2
\$\begingroup\$

Your code obfuscates the goal, partially because of a generic non-telling function name.
And it needlessly scans the entire input array for each value in [min, max] range.

Basically, the goal is to list values in [min, max] range that aren't present in the input array:

let rangeExceptInput = (min, max, input) => {
    let range = new Array(max - min + 1).fill(0).map((e, index) => min + index);
    let inputInRange = new Set(input.filter(e => e >= min && e <= max));
    return range.filter(e => !inputInRange.has(e));
}

ES6 Set is used to speed up the lookup of existing values.

This approach isn't super efficient though, so if JS profiler shows considerable time spent here, you can implement it differently, for example, by using a sorted inRange and plain for-loops.

\$\endgroup\$

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