Fix(curriculum): improve dice game tests for straights (#58627)

This commit is contained in:
Raymond Liu 2025-02-20 04:29:35 -08:00 committed by GitHub
parent b26231e1df
commit cecd65719b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,7 +9,7 @@ dashedName: step-14
For the last portion of the game, you will need to create an algorithm that checks for the presence of a straight. A small straight is when four of the dice have consecutive values in any order (Ex. in a roll of `41423`, we have `1234`) resulting in a score of `30` points. A large straight is when all five dice have consecutive values in any order (Ex. in a roll of `35124`, we have `12345`) resulting in a score of `40` points.
Declare a `checkForStraights` function which accepts an array of numbers. If the user gets a large straight, update the fifth radio button with a score of `40`. If the user gets a small straight, update the fourth radio button with a score of `30`. If the user gets no straight, update the last radio button to display `0`.
Declare a `checkForStraights` function which accepts an array of numbers. If the user gets a large straight, update the fifth radio button with a score of `40`. If the user gets a small straight, update the fourth radio button with a score of `30`. Regardless, it should always update the last radio button to display a score of 0, with the correct attributes.
Call your `checkForStraights` function when the `rollDiceBtn` is clicked to complete your dice game!
@ -21,39 +21,59 @@ Your `checkForStraights` variable should be a function.
assert.isFunction(checkForStraights);
```
If a small straight is rolled, your `checkForStraights` function should enable the fourth radio button, set the value to `30`, and update the displayed text to `, score = 30`.
If a small straight is rolled, your `checkForStraights` function should enable the fourth radio button, set the value to `30`, update the displayed text to `, score = 30` and leave the fifth radio button disabled.
```js
resetRadioOptions();
checkForStraights([4,2,5,3,5]);
assert.isTrue(scoreInputs[4].disabled);
assert.isFalse(scoreInputs[3].disabled);
assert.strictEqual(scoreInputs[3].value, "30");
assert.strictEqual(scoreSpans[3].innerText, ", score = 30");
const assertSmallStraight = (_diceValuesArr) => {
resetRadioOptions();
checkForStraights(_diceValuesArr);
assert.isTrue(scoreInputs[4].disabled);
assert.isFalse(scoreInputs[3].disabled);
assert.strictEqual(scoreInputs[3].value, "30");
assert.strictEqual(scoreSpans[3].innerText, ", score = 30");
}
// Basic straights
assertSmallStraight([1,1,2,3,4])
assertSmallStraight([2,3,4,5,5])
assertSmallStraight([3,4,5,6,6])
// 5 unique numbers, but only small straight
assertSmallStraight([1,2,3,4,6])
assertSmallStraight([1,3,4,5,6])
// Straights with duplicates in middle
assertSmallStraight([1,2,2,3,4])
assertSmallStraight([2,3,3,4,5])
assertSmallStraight([3,4,5,5,6])
// Out of order straights
assertSmallStraight([1,3,2,1,4])
assertSmallStraight([5,4,3,3,2])
assertSmallStraight([3,4,5,6,1])
```
If a large straight is rolled, your `checkForStraights` function should enable the fifth radio button, set the value to `40`, and update the displayed text to `, score = 40`.
If a large straight is rolled, your `checkForStraights` function should enable the fourth button, set the value to `30`, update the displayed text to `, score = 30`. Additionally, the function should enable the fifth radio button, set the value to `40`, and update the displayed text to `, score = 40`.
```js
resetRadioOptions();
checkForStraights([4,2,5,3,1]);
assert.isFalse(scoreInputs[4].disabled);
assert.strictEqual(scoreInputs[4].value, "40");
assert.strictEqual(scoreSpans[4].innerText, ", score = 40");
const assertLargeStraight = (_diceValuesArr) => {
resetRadioOptions();
checkForStraights(_diceValuesArr);
assert.isFalse(scoreInputs[3].disabled);
assert.strictEqual(scoreInputs[3].value, "30");
assert.strictEqual(scoreSpans[3].innerText, ", score = 30");
assert.isFalse(scoreInputs[4].disabled);
assert.strictEqual(scoreInputs[4].value, "40");
assert.strictEqual(scoreSpans[4].innerText, ", score = 40");
}
// Basic straights
assertLargeStraight([1,2,3,4,5])
assertLargeStraight([2,3,4,5,6])
// Backward straights
assertLargeStraight([5,4,3,2,1])
assertLargeStraight([6,5,4,3,2])
// Out of order straights
assertLargeStraight([1,5,3,4,2])
assertLargeStraight([2,4,6,5,3])
```
If a large straight is rolled, your `checkForStraights` function should also enable the fourth radio button, set the value to `30`, and update the displayed text to `, score = 30`.
```js
resetRadioOptions();
checkForStraights([4,2,5,3,6]);
assert.isFalse(scoreInputs[4].disabled);
assert.isFalse(scoreInputs[3].disabled);
assert.strictEqual(scoreInputs[3].value, "30");
assert.strictEqual(scoreSpans[3].innerText, ", score = 30");
```
If no straight is rolled, your `checkForStraights` function should enable the final radio button, set the value to `0`, and update the displayed text to `, score = 0`.
If no straight is rolled, your `checkForStraights` function should not enable the fourth or fifth radio button.
```js
const assertNoStraight = (_diceValuesArr) => {
@ -61,13 +81,33 @@ const assertNoStraight = (_diceValuesArr) => {
checkForStraights(_diceValuesArr);
assert.isTrue(scoreInputs[3].disabled);
assert.isTrue(scoreInputs[4].disabled);
assert.isFalse(scoreInputs[5].disabled);
assert.strictEqual(scoreInputs[5].value, "0");
assert.strictEqual(scoreSpans[5].innerText, ", score = 0");
}
// Simple cases
assertNoStraight([1,4,4,4,4]);
assertNoStraight([2,2,3,3,3]);
assertNoStraight([5,5,5,5,5]);
assertNoStraight([6,5,1,5,6]);
// Almost a straight, but broken in middle
assertNoStraight([1,2,3,5,6]);
assertNoStraight([1,2,4,5,6]);
// Almost a straight with duplicates in middle
assertNoStraight([1,2,2,3,5]);
assertNoStraight([2,4,4,5,6]);
// Repeat of last 4 cases, but not in order
assertNoStraight([1,5,6,2,3]);
assertNoStraight([5,2,4,1,6]);
assertNoStraight([2,1,5,3,2]);
assertNoStraight([2,4,5,4,6]);
```
assertNoStraight([1,1,1,1,1]);
assertNoStraight([1,1,4,4,4]);
If no straight is rolled, your `checkForStraights` function should enable the final radio button set the value to `0`, and update the displayed text to `, score = 0`.
```js
resetRadioOptions();
checkForStraights([1,1,1,1,1]);
assert.isFalse(scoreInputs[5].disabled);
assert.strictEqual(scoreInputs[5].value, "0");
assert.strictEqual(scoreSpans[5].innerText, ", score = 0");
```
You should call the `checkForStraights` function when the `rollDiceBtn` is clicked.