fix(curriculum): restore Array.prototype.sort after test (#58115)

This commit is contained in:
benoit74
2025-01-15 08:59:01 +01:00
committed by GitHub
parent 04574b7d11
commit bbeb440658
5 changed files with 55 additions and 25 deletions
@@ -82,7 +82,7 @@ assert.sameMembers(
`bubbleSort` should not use the built-in `.sort()` method.
```js
assert(isBuiltInSortUsed());
assert.isFalse(isBuiltInSortUsed());
```
# --seed--
@@ -99,9 +99,14 @@ function isSorted(a){
function isBuiltInSortUsed(){
let sortUsed = false;
const temp = Array.prototype.sort;
Array.prototype.sort = () => sortUsed = true;
bubbleSort([0, 1]);
return !sortUsed;
try {
bubbleSort([0, 1]);
} finally {
Array.prototype.sort = temp;
}
return sortUsed;
}
```
@@ -84,7 +84,7 @@ assert.deepEqual(insertionSort([5, 4, 33, 2, 8]), [2, 4, 5, 8, 33])
`insertionSort` should not use the built-in `.sort()` method.
```js
assert(isBuiltInSortUsed());
assert.isFalse(isBuiltInSortUsed());
```
# --seed--
@@ -101,9 +101,14 @@ function isSorted(a){
function isBuiltInSortUsed(){
let sortUsed = false;
const temp = Array.prototype.sort;
Array.prototype.sort = () => sortUsed = true;
insertionSort([0, 1]);
return !sortUsed;
try {
insertionSort([0, 1]);
} finally {
Array.prototype.sort = temp;
}
return sortUsed;
}
```
@@ -31,13 +31,6 @@ assert.isFunction(mergeSort);
`mergeSort` should return a sorted array (least to greatest).
```js
function isSorted(a){
for(let i = 0; i < a.length - 1; i++)
if(a[i] > a[i + 1])
return false;
return true;
}
assert.isTrue(
isSorted(
mergeSort([
@@ -93,17 +86,34 @@ assert.sameMembers(
`mergeSort` should not use the built-in `.sort()` method.
```js
function isBuiltInSortUsed(){
let sortUsed = false;
Array.prototype.sort = () => sortUsed = true;
mergeSort([0, 1]);
return sortUsed;
}
assert.isFalse(isBuiltInSortUsed());
```
# --seed--
## --after-user-code--
```js
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;
const temp = Array.prototype.sort;
Array.prototype.sort = () => sortUsed = true;
try {
mergeSort([0, 1]);
} finally {
Array.prototype.sort = temp;
}
return sortUsed;
}
```
## --seed-contents--
```js
@@ -80,7 +80,7 @@ assert.sameMembers(
`quickSort` should not use the built-in `.sort()` method.
```js
assert(isBuiltInSortUsed());
assert.isFalse(isBuiltInSortUsed());
```
# --seed--
@@ -97,9 +97,14 @@ function isSorted(a){
function isBuiltInSortUsed(){
let sortUsed = false;
const temp = Array.prototype.sort;
Array.prototype.sort = () => sortUsed = true;
quickSort([0, 1]);
return !sortUsed;
try {
quickSort([0, 1]);
} finally {
Array.prototype.sort = temp;
}
return sortUsed;
}
```
@@ -78,7 +78,7 @@ assert.sameMembers(
`selectionSort` should not use the built-in `.sort()` method.
```js
assert(isBuiltInSortUsed());
assert.isFalse(isBuiltInSortUsed());
```
# --seed--
@@ -95,9 +95,14 @@ function isSorted(a){
function isBuiltInSortUsed(){
let sortUsed = false;
const temp = Array.prototype.sort;
Array.prototype.sort = () => sortUsed = true;
selectionSort([0, 1]);
return !sortUsed;
try {
selectionSort([0, 1]);
} finally {
Array.prototype.sort = temp;
}
return sortUsed;
}
```