fix: Replace Array.prototype.sort and update old isSorted method. (#39360)
* fix: Replace Array.prototype.sort and update old isSorted method. * fix: Change name of function from 'checkInBuiltSort' to 'checkBuilitInSort'. * fix: Change name of function from 'checkBuilitInSort' to 'isBuiltInSortUsed'.
This commit is contained in:
		| @@ -39,9 +39,11 @@ tests: | |||||||
|  |  | ||||||
| ```js | ```js | ||||||
| // check if array is sorted | // check if array is sorted | ||||||
| function isSorted(arr) { | function isSorted(a){ | ||||||
|   var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1); |   for(let i = 0; i < a.length - 1; i++) | ||||||
|   return check(0); |     if(a[i] > a[i + 1]) | ||||||
|  |       return false; | ||||||
|  |   return true; | ||||||
| } | } | ||||||
| // generate a randomly filled array | // generate a randomly filled array | ||||||
| var array = new Array(); | var array = new Array(); | ||||||
| @@ -68,4 +70,5 @@ var MinHeap = function() { | |||||||
| ```js | ```js | ||||||
| // solution required | // solution required | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| </section> | </section> | ||||||
|   | |||||||
| @@ -40,9 +40,11 @@ tests: | |||||||
|  |  | ||||||
| ```js | ```js | ||||||
| // check if array is sorted | // check if array is sorted | ||||||
| function isSorted(arr) { | function isSorted(a){ | ||||||
|   var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1); |   for(let i = 0; i < a.length - 1; i++) | ||||||
|   return check(0); |     if(a[i] > a[i + 1]) | ||||||
|  |       return false; | ||||||
|  |   return true; | ||||||
| } | } | ||||||
| // generate a randomly filled array | // generate a randomly filled array | ||||||
| var array = new Array(); | var array = new Array(); | ||||||
| @@ -69,4 +71,5 @@ var MinHeap = function() { | |||||||
| ```js | ```js | ||||||
| // solution required | // solution required | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| </section> | </section> | ||||||
|   | |||||||
| @@ -32,7 +32,7 @@ tests: | |||||||
|   - text: <code>bubbleSort</code> should return an array that is unchanged except for order. |   - text: <code>bubbleSort</code> should return an array that is unchanged except for order. | ||||||
|     testString: assert.sameMembers(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); |     testString: assert.sameMembers(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); | ||||||
|   - text: <code>bubbleSort</code> should not use the built-in <code>.sort()</code> method. |   - text: <code>bubbleSort</code> should not use the built-in <code>.sort()</code> method. | ||||||
|     testString: assert(!code.match(/\.\s*sort\s*\(/)); |     testString: assert(isBuiltInSortUsed()); | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| @@ -60,10 +60,19 @@ bubbleSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]) | |||||||
| <div id='js-teardown'> | <div id='js-teardown'> | ||||||
|  |  | ||||||
| ```js | ```js | ||||||
| function isSorted(arr) { | function isSorted(a){ | ||||||
|   var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1); |   for(let i = 0; i < a.length - 1; i++) | ||||||
|   return check(0); |     if(a[i] > a[i + 1]) | ||||||
| }; |       return false; | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function isBuiltInSortUsed(){ | ||||||
|  |   let sortUsed = false; | ||||||
|  |   Array.prototype.sort = () => sortUsed = true; | ||||||
|  |   bubbleSort([0, 1]); | ||||||
|  |   return !sortUsed; | ||||||
|  | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| </div> | </div> | ||||||
|   | |||||||
| @@ -30,7 +30,7 @@ tests: | |||||||
|   - text: <code>insertionSort</code> should return an array that is unchanged except for order. |   - text: <code>insertionSort</code> should return an array that is unchanged except for order. | ||||||
|     testString: assert.sameMembers(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); |     testString: assert.sameMembers(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); | ||||||
|   - text: <code>insertionSort</code> should not use the built-in <code>.sort()</code> method. |   - text: <code>insertionSort</code> should not use the built-in <code>.sort()</code> method. | ||||||
|     testString: assert(!code.match(/\.\s*sort\s*\(/)); |     testString: assert(isBuiltInSortUsed()); | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| @@ -58,10 +58,19 @@ insertionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 9 | |||||||
| <div id='js-teardown'> | <div id='js-teardown'> | ||||||
|  |  | ||||||
| ```js | ```js | ||||||
| function isSorted(arr) { | function isSorted(a){ | ||||||
|   var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1); |   for(let i = 0; i < a.length - 1; i++) | ||||||
|   return check(0); |     if(a[i] > a[i + 1]) | ||||||
| }; |       return false; | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function isBuiltInSortUsed(){ | ||||||
|  |   let sortUsed = false; | ||||||
|  |   Array.prototype.sort = () => sortUsed = true; | ||||||
|  |   insertionSort([0, 1]); | ||||||
|  |   return !sortUsed; | ||||||
|  | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| </div> | </div> | ||||||
|   | |||||||
| @@ -34,7 +34,7 @@ tests: | |||||||
|   - text: <code>mergeSort</code> should return an array that is unchanged except for order. |   - text: <code>mergeSort</code> should return an array that is unchanged except for order. | ||||||
|     testString: assert.sameMembers(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); |     testString: assert.sameMembers(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); | ||||||
|   - text: <code>mergeSort</code> should not use the built-in <code>.sort()</code> method. |   - text: <code>mergeSort</code> should not use the built-in <code>.sort()</code> method. | ||||||
|     testString: assert(!removeJSComments(code).match(/\.\s*sort\s*\(/)); |     testString: assert(isBuiltInSortUsed()); | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| @@ -63,12 +63,19 @@ mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]); | |||||||
| <div id='js-teardown'> | <div id='js-teardown'> | ||||||
|  |  | ||||||
| ```js | ```js | ||||||
| const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, ''); | function isSorted(a){ | ||||||
|  |   for(let i = 0; i < a.length - 1; i++) | ||||||
|  |     if(a[i] > a[i + 1]) | ||||||
|  |       return false; | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
| function isSorted(arr) { | function isBuiltInSortUsed(){ | ||||||
|   var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1); |   let sortUsed = false; | ||||||
|   return check(0); |   Array.prototype.sort = () => sortUsed = true; | ||||||
| }; |   mergeSort([0, 1]); | ||||||
|  |   return !sortUsed; | ||||||
|  | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| </div> | </div> | ||||||
|   | |||||||
| @@ -31,7 +31,7 @@ tests: | |||||||
|   - text: <code>quickSort</code> should return an array that is unchanged except for order. |   - text: <code>quickSort</code> should return an array that is unchanged except for order. | ||||||
|     testString: assert.sameMembers(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); |     testString: assert.sameMembers(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); | ||||||
|   - text: <code>quickSort</code> should not use the built-in <code>.sort()</code> method. |   - text: <code>quickSort</code> should not use the built-in <code>.sort()</code> method. | ||||||
|     testString: assert(!code.match(/\.?[\s\S]*?sort/)); |     testString: assert(isBuiltInSortUsed()); | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| @@ -61,10 +61,19 @@ function quickSort(array) { | |||||||
| <div id='js-teardown'> | <div id='js-teardown'> | ||||||
|  |  | ||||||
| ```js | ```js | ||||||
| function isSorted(arr) { | function isSorted(a){ | ||||||
|   var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1); |   for(let i = 0; i < a.length - 1; i++) | ||||||
|   return check(0); |     if(a[i] > a[i + 1]) | ||||||
| }; |       return false; | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function isBuiltInSortUsed(){ | ||||||
|  |   let sortUsed = false; | ||||||
|  |   Array.prototype.sort = () => sortUsed = true; | ||||||
|  |   quickSort([0, 1]); | ||||||
|  |   return !sortUsed; | ||||||
|  | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| </div> | </div> | ||||||
|   | |||||||
| @@ -30,7 +30,7 @@ tests: | |||||||
|   - text: <code>selectionSort</code> should return an array that is unchanged except for order. |   - text: <code>selectionSort</code> should return an array that is unchanged except for order. | ||||||
|     testString: assert.sameMembers(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); |     testString: assert.sameMembers(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); | ||||||
|   - text: <code>selectionSort</code> should not use the built-in <code>.sort()</code> method. |   - text: <code>selectionSort</code> should not use the built-in <code>.sort()</code> method. | ||||||
|     testString: assert(!code.match(/\.\s*sort\s*\(/)); |     testString: assert(isBuiltInSortUsed()); | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| @@ -59,10 +59,19 @@ selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 9 | |||||||
| <div id='js-teardown'> | <div id='js-teardown'> | ||||||
|  |  | ||||||
| ```js | ```js | ||||||
| function isSorted(arr) { | function isSorted(a){ | ||||||
|   var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1); |   for(let i = 0; i < a.length - 1; i++) | ||||||
|   return check(0); |     if(a[i] > a[i + 1]) | ||||||
| }; |       return false; | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function isBuiltInSortUsed(){ | ||||||
|  |   let sortUsed = false; | ||||||
|  |   Array.prototype.sort = () => sortUsed = true; | ||||||
|  |   selectionSort([0, 1]); | ||||||
|  |   return !sortUsed; | ||||||
|  | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| </div> | </div> | ||||||
|   | |||||||
| @@ -43,10 +43,11 @@ tests: | |||||||
|  |  | ||||||
| ```js | ```js | ||||||
| // check if array is sorted | // check if array is sorted | ||||||
| function isSorted(arr) { | function isSorted(a){ | ||||||
|   var check = i => |   for(let i = 0; i < a.length - 1; i++) | ||||||
|     i == arr.length - 1 ? true : arr[i] > arr[i + 1] ? false : check(i + 1); |     if(a[i] > a[i + 1]) | ||||||
|   return check(0); |       return false; | ||||||
|  |   return true; | ||||||
| } | } | ||||||
| // generate a randomly filled array | // generate a randomly filled array | ||||||
| var array = new Array(); | var array = new Array(); | ||||||
|   | |||||||
| @@ -92,7 +92,9 @@ function permutationSort(arr) { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   function isSorted(a){ |   function isSorted(a){ | ||||||
|     for (var i = 1; i < a.length; i++) if (a[i - 1] > a[i]) return false; |     for(let i = 0; i < a.length - 1; i++) | ||||||
|  |       if(a[i] > a[i + 1]) | ||||||
|  |         return false; | ||||||
|     return true; |     return true; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -40,9 +40,11 @@ tests: | |||||||
|  |  | ||||||
| ```js | ```js | ||||||
| // check if array is sorted | // check if array is sorted | ||||||
| function isSorted(arr) { | function isSorted(a){ | ||||||
|   var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1); |   for(let i = 0; i < a.length - 1; i++) | ||||||
|   return check(0); |     if(a[i] > a[i + 1]) | ||||||
|  |       return false; | ||||||
|  |   return true; | ||||||
| } | } | ||||||
| // generate a randomly filled array | // generate a randomly filled array | ||||||
| var array = new Array(); | var array = new Array(); | ||||||
| @@ -69,4 +71,5 @@ var MinHeap = function() { | |||||||
| ```js | ```js | ||||||
| // solution required | // solution required | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| </section> | </section> | ||||||
|   | |||||||
| @@ -29,7 +29,7 @@ tests: | |||||||
|   - text: <code>bubbleSort</code> returns an array that is unchanged except for order. |   - text: <code>bubbleSort</code> returns an array that is unchanged except for order. | ||||||
|     testString: assert.sameMembers(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); |     testString: assert.sameMembers(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); | ||||||
|   - text: <code>bubbleSort</code> should not use the built-in <code>.sort()</code> method. |   - text: <code>bubbleSort</code> should not use the built-in <code>.sort()</code> method. | ||||||
|     testString: assert.strictEqual(code.search(/\.sort\(/), -1); |     testString: assert(isBuiltInSortUsed()); | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| @@ -57,11 +57,19 @@ bubbleSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]) | |||||||
| <div id='js-teardown'> | <div id='js-teardown'> | ||||||
|  |  | ||||||
| ```js | ```js | ||||||
| function isSorted(arr) { | function isSorted(a){ | ||||||
|   var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1); |   for(let i = 0; i < a.length - 1; i++) | ||||||
|   return check(0); |     if(a[i] > a[i + 1]) | ||||||
| }; |       return false; | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function isBuiltInSortUsed(){ | ||||||
|  |   let sortUsed = false; | ||||||
|  |   Array.prototype.sort = () => sortUsed = true; | ||||||
|  |   bubbleSort([0, 1]); | ||||||
|  |   return !sortUsed; | ||||||
|  | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| </div> | </div> | ||||||
|   | |||||||
| @@ -28,7 +28,7 @@ tests: | |||||||
|   - text: <code>insertionSort</code> returns an array that is unchanged except for order. |   - text: <code>insertionSort</code> returns an array that is unchanged except for order. | ||||||
|     testString: assert.sameMembers(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); |     testString: assert.sameMembers(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); | ||||||
|   - text: <code>insertionSort</code> should not use the built-in <code>.sort()</code> method. |   - text: <code>insertionSort</code> should not use the built-in <code>.sort()</code> method. | ||||||
|     testString: assert.strictEqual(code.search(/\.sort\(/), -1); |     testString: assert(isBuiltInSortUsed()); | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| @@ -56,11 +56,19 @@ insertionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 9 | |||||||
| <div id='js-teardown'> | <div id='js-teardown'> | ||||||
|  |  | ||||||
| ```js | ```js | ||||||
| function isSorted(arr) { | function isSorted(a){ | ||||||
|   var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1); |   for(let i = 0; i < a.length - 1; i++) | ||||||
|   return check(0); |     if(a[i] > a[i + 1]) | ||||||
| }; |       return false; | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function isBuiltInSortUsed(){ | ||||||
|  |   let sortUsed = false; | ||||||
|  |   Array.prototype.sort = () => sortUsed = true; | ||||||
|  |   insertionSort([0, 1]); | ||||||
|  |   return !sortUsed; | ||||||
|  | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| </div> | </div> | ||||||
|   | |||||||
| @@ -33,7 +33,7 @@ tests: | |||||||
|   - text: <code>mergeSort</code> returns an array that is unchanged except for order. |   - text: <code>mergeSort</code> returns an array that is unchanged except for order. | ||||||
|     testString: assert.sameMembers(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); |     testString: assert.sameMembers(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); | ||||||
|   - text: <code>mergeSort</code> should not use the built-in <code>.sort()</code> method. |   - text: <code>mergeSort</code> should not use the built-in <code>.sort()</code> method. | ||||||
|     testString: assert.strictEqual(code.search(/\.sort\(/), -1); |     testString: assert(isBuiltInSortUsed()); | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| @@ -62,11 +62,19 @@ mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]); | |||||||
| <div id='js-teardown'> | <div id='js-teardown'> | ||||||
|  |  | ||||||
| ```js | ```js | ||||||
| function isSorted(arr) { | function isSorted(a){ | ||||||
|   var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1); |   for(let i = 0; i < a.length - 1; i++) | ||||||
|   return check(0); |     if(a[i] > a[i + 1]) | ||||||
| }; |       return false; | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function isBuiltInSortUsed(){ | ||||||
|  |   let sortUsed = false; | ||||||
|  |   Array.prototype.sort = () => sortUsed = true; | ||||||
|  |   mergeSort([0, 1]); | ||||||
|  |   return !sortUsed; | ||||||
|  | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| </div> | </div> | ||||||
|   | |||||||
| @@ -28,7 +28,7 @@ tests: | |||||||
|   - text: <code>quickSort</code> returns an array that is unchanged except for order. |   - text: <code>quickSort</code> returns an array that is unchanged except for order. | ||||||
|     testString: assert.sameMembers(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); |     testString: assert.sameMembers(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); | ||||||
|   - text: <code>quickSort</code> should not use the built-in <code>.sort()</code> method. |   - text: <code>quickSort</code> should not use the built-in <code>.sort()</code> method. | ||||||
|     testString: assert.strictEqual(code.search(/\.sort\(/), -1); |     testString: assert(isBuiltInSortUsed()); | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| @@ -58,11 +58,19 @@ function quickSort(array) { | |||||||
| <div id='js-teardown'> | <div id='js-teardown'> | ||||||
|  |  | ||||||
| ```js | ```js | ||||||
| function isSorted(arr) { | function isSorted(a){ | ||||||
|   var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1); |   for(let i = 0; i < a.length - 1; i++) | ||||||
|   return check(0); |     if(a[i] > a[i + 1]) | ||||||
| }; |       return false; | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function isBuiltInSortUsed(){ | ||||||
|  |   let sortUsed = false; | ||||||
|  |   Array.prototype.sort = () => sortUsed = true; | ||||||
|  |   quickSort([0, 1]); | ||||||
|  |   return !sortUsed; | ||||||
|  | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| </div> | </div> | ||||||
|   | |||||||
| @@ -28,7 +28,7 @@ tests: | |||||||
|   - text: <code>selectionSort</code> returns an array that is unchanged except for order. |   - text: <code>selectionSort</code> returns an array that is unchanged except for order. | ||||||
|     testString: assert.sameMembers(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); |     testString: assert.sameMembers(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]); | ||||||
|   - text: <code>selectionSort</code> should not use the built-in <code>.sort()</code> method. |   - text: <code>selectionSort</code> should not use the built-in <code>.sort()</code> method. | ||||||
|     testString: assert.strictEqual(code.search(/\.sort\(/), -1); |     testString: assert(isBuiltInSortUsed()); | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| @@ -57,11 +57,19 @@ selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 9 | |||||||
| <div id='js-teardown'> | <div id='js-teardown'> | ||||||
|  |  | ||||||
| ```js | ```js | ||||||
| function isSorted(arr) { | function isSorted(a){ | ||||||
|   var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1); |   for(let i = 0; i < a.length - 1; i++) | ||||||
|   return check(0); |     if(a[i] > a[i + 1]) | ||||||
| }; |       return false; | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function isBuiltInSortUsed(){ | ||||||
|  |   let sortUsed = false; | ||||||
|  |   Array.prototype.sort = () => sortUsed = true; | ||||||
|  |   selectionSort([0, 1]); | ||||||
|  |   return !sortUsed; | ||||||
|  | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| </div> | </div> | ||||||
|   | |||||||
| @@ -43,10 +43,11 @@ tests: | |||||||
|  |  | ||||||
| ```js | ```js | ||||||
| // check if array is sorted | // check if array is sorted | ||||||
| function isSorted(arr) { | function isSorted(a){ | ||||||
|   var check = i => |   for(let i = 0; i < a.length - 1; i++) | ||||||
|     i == arr.length - 1 ? true : arr[i] > arr[i + 1] ? false : check(i + 1); |     if(a[i] > a[i + 1]) | ||||||
|   return check(0); |       return false; | ||||||
|  |   return true; | ||||||
| } | } | ||||||
| // generate a randomly filled array | // generate a randomly filled array | ||||||
| var array = new Array(); | var array = new Array(); | ||||||
|   | |||||||
| @@ -40,9 +40,11 @@ tests: | |||||||
|  |  | ||||||
| ```js | ```js | ||||||
| // check if array is sorted | // check if array is sorted | ||||||
| function isSorted(arr) { | function isSorted(a){ | ||||||
|   var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1); |   for(let i = 0; i < a.length - 1; i++) | ||||||
|   return check(0); |     if(a[i] > a[i + 1]) | ||||||
|  |       return false; | ||||||
|  |   return true; | ||||||
| } | } | ||||||
| // generate a randomly filled array | // generate a randomly filled array | ||||||
| var array = new Array(); | var array = new Array(); | ||||||
| @@ -69,4 +71,5 @@ var MinHeap = function() { | |||||||
| ```js | ```js | ||||||
| // solution required | // solution required | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| </section> | </section> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user