title: Javascript sort doesn't work when i'm using it from another function
body: <p><br>
  I'm new in Javascript and getting this weird issue:<br>
  When calling "bubbleSort" function from another "sort" function which supposed to clone the array and return a new sorted array - the result i get is not right.
  <br></p>
  
  <p>I'll be happy to know what is the problem, I've been trying other ways to clone the array such as while loop, slice, [...a] and others and still can't understand what the problem is.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function bubbleSort(arr, compareFunction = comparator()) {
    var len = arr.length;
    console.log("bubble!!");
    for (var i = 0; i &lt; len; i++) {
      for (var j = 0; j &lt; len - i - 1; j++) {
        if (comparator(a[j], a[j + 1]) == 1) {
          var temp = arr[j];
          arr[j] = arr[j + 1];
          arr[j + 1] = temp;
        }
      }
    }
    return arr;
  }
  function sort(a, sorter, compareFunction = comparator()) {
    return sorter(a, comparator);
  }
  function sortNumbers(a) {
    var arrClone = a.slice(0);
    arrClone = sort(arrClone, bubbleSort);
    return arrClone;
  }
  function comparator(a, b) { if (a &lt; b) return -1; else if (a &gt; b) return 1; else return 0; }
  
  
  // when i'm using any of the following:
  var a = [33, 103, 3, 726, 200, 984, 198, 764, 9];
  console.log(bubbleSort(a));
  a = [33, 103, 3, 726, 200, 984, 198, 764, 9];
  console.log(bubbleSort(a, comparator));
  a = [33, 103, 3, 726, 200, 984, 198, 764, 9];
  console.log(sort(a, bubbleSort));
  // I'm getting the desired result:
  // [ 3, 9, 33, 103, 198, 200, 726, 764, 984 ]
  
  // But when i'm using:
  
  a = [33, 103, 3, 726, 200, 984, 198, 764, 9];
  console.log(sortNumbers(a));
  // I'm getting:
  // [ 33, 3, 103, 200, 726, 198, 984, 9, 764 ]</code></pre>
  </div>
  </div>
  </p>
  
  <p>when i'm using any of the following:</p>
  
  <pre><code>var a = [33, 103, 3, 726, 200, 984, 198, 764, 9];
  console.log(bubbleSort(a));
  console.log(bubbleSort(a, comparator));
  console.log(sort(a, bubbleSort));
  </code></pre>
  
  <p>I'm getting the desired result:</p>
  
  <pre><code> [ 3, 9, 33, 103, 198, 200, 726, 764, 984 ]
  </code></pre>
  
  <p>But when i'm using:</p>
  
  <pre><code>console.log(sortNumbers(a));
  </code></pre>
  
  <p>I'm getting:</p>
  
  <pre><code>[ 33, 3, 103, 200, 726, 198, 984, 9, 764 ]
  </code></pre>