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:
Kartik Soneji
2020-08-11 02:01:18 +05:30
committed by GitHub
parent 8996fa7502
commit f813dfff87
17 changed files with 172 additions and 73 deletions

View File

@ -29,7 +29,7 @@ tests:
- 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]);
- 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'>
```js
function isSorted(arr) {
var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);
return check(0);
};
function isSorted(a){
for(let i = 0; i < a.length - 1; i++)
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>

View File

@ -28,7 +28,7 @@ tests:
- 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]);
- 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'>
```js
function isSorted(arr) {
var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);
return check(0);
};
function isSorted(a){
for(let i = 0; i < a.length - 1; i++)
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>

View File

@ -33,7 +33,7 @@ tests:
- 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]);
- 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'>
```js
function isSorted(arr) {
var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);
return check(0);
};
function isSorted(a){
for(let i = 0; i < a.length - 1; i++)
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>

View File

@ -28,7 +28,7 @@ tests:
- 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]);
- 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'>
```js
function isSorted(arr) {
var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);
return check(0);
};
function isSorted(a){
for(let i = 0; i < a.length - 1; i++)
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>

View File

@ -28,7 +28,7 @@ tests:
- 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]);
- 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'>
```js
function isSorted(arr) {
var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);
return check(0);
};
function isSorted(a){
for(let i = 0; i < a.length - 1; i++)
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>