[Solved] How to improve this function to test Underscore Sample `_.sample()`


Due to the complaints that this question was not very direct I created other posts to get the answers I was looking for. This answers the main questions I had.

Answers:

  1. Understanding how array.prototype.call() works explains the output of this pattern. A full detailed answer can be found here: Mapping Array in Javascript with sequential numbers
  2. The reason why is because of how array.prototype.map() works – full answer here: Why do we use Number.prototype.valueOf inside of a map() function
  3. Yes, as of ES6 there is an alternate way to set a default for an argument in javascript:
(function(arraySize = 10, timesToRun = 1000){
  let myArray = Array.apply(null, {length: arraySize}).map(Number.call, Number);
  let resultsArray = Array.apply(null, Array(arraySize)).map(Number.prototype.valueOf,0);
  for (let i = 0; i < timesToRun; i++) {
    var result = _.sample(myArray);
    resultsArray[result]++;
  }
  // Turned into console.log() for StackOverflow Snippet
  // return resultsArray;
  console.log(resultsArray);
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

And here is a simple way to set this up graphically if anyone is interested.

solved How to improve this function to test Underscore Sample `_.sample()`