From 198523c7783b7874cd6245bc7ca3b1bf65c7b99c Mon Sep 17 00:00:00 2001 From: Michael Ehme <99354721+mcehme@users.noreply.github.com> Date: Tue, 16 Jul 2024 03:27:18 -0400 Subject: [PATCH] fix(curriculum): removing long running loop in test by overriding Math.random (#55499) Co-authored-by: Jeremy L Thompson --- .../657e0c2c6a9d37705146f34d.md | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/review-algorithmic-thinking-by-building-a-dice-game/657e0c2c6a9d37705146f34d.md b/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/review-algorithmic-thinking-by-building-a-dice-game/657e0c2c6a9d37705146f34d.md index a7949dc5d04..866420a8a2f 100644 --- a/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/review-algorithmic-thinking-by-building-a-dice-game/657e0c2c6a9d37705146f34d.md +++ b/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/review-algorithmic-thinking-by-building-a-dice-game/657e0c2c6a9d37705146f34d.md @@ -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--