mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-30 16:01:14 -04:00
fix(curriculum): removing long running loop in test by overriding Math.random (#55499)
Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org>
This commit is contained in:
@@ -45,19 +45,24 @@ assert.strictEqual(scoreSpans[5].innerText, ", score = 0");
|
||||
You should call your `detectFullHouse` function when your `rollDiceBtn` is clicked.
|
||||
|
||||
```js
|
||||
const counts = (arr) => arr.reduce((counts, num) => {counts[num] = ++counts[num] || 1; return counts}, {});
|
||||
const isFullHouse = (arr) => arr.includes(3) && arr.includes(2) && arr.length === 2;
|
||||
// Absurdly high, but will either timeout or bail early when assertion runs.
|
||||
for (let i = 0; i < 100_000; i++) {
|
||||
rolls = 0;
|
||||
// Temporarily modifies Math.random to guarantee full house.
|
||||
const origMathRandom = Math.random
|
||||
const myMathRandom = (() => {
|
||||
let i = 0;
|
||||
const values = [0, 0, 0, 1 / 6 + 0.000001, 1 / 6 + 0.000001];
|
||||
return () => values[i++ % 5];
|
||||
})();
|
||||
|
||||
Math.random = myMathRandom;
|
||||
try {
|
||||
rollDiceBtn.click();
|
||||
if (isFullHouse(diceValuesArr)) {
|
||||
assert.isFalse(scoreInputs[2].disabled);
|
||||
assert.strictEqual(scoreInputs[2].value, "25");
|
||||
assert.strictEqual(scoreSpans[2].innerText, ", score = 25");
|
||||
break;
|
||||
}
|
||||
} finally {
|
||||
Math.random = origMathRandom;
|
||||
}
|
||||
|
||||
assert.isFalse(scoreInputs[2].disabled);
|
||||
assert.strictEqual(scoreInputs[2].value, "25");
|
||||
assert.strictEqual(scoreSpans[2].innerText, ", score = 25");
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
Reference in New Issue
Block a user