Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

"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 = 0.

“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.”

Isn’t that the case here?..

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

  
  const checkForStraights = (arr) => {
  const sortedUniqueArr = [...new Set(arr)].sort((a, b) => a - b);

  const smallStraights = [
    [1, 2, 3, 4],
    [2, 3, 4, 5],
    [3, 4, 5, 6]
  ];
  const largeStraight = [
    [1, 2, 3, 4, 5],
    [2, 3, 4, 5, 6]
    ];

  if (sortedUniqueArr.join() === largeStraight.join()) {
    updateRadioOption(4, 40);
    return;
  }

  for (const straight of smallStraights) {
    if (straight.every(num => sortedUniqueArr.includes(num))) {
      updateRadioOption(3, 30);
      return;
    }
  }

  updateRadioOption(5, 0);
};

rollDiceBtn.addEventListener("click", () => {
  if (rolls === 3) {
    alert("You have made three rolls this round. Please select a score.");
  } else {
    rolls++;
    resetRadioOptions();
    rollDice();
    updateStats();
    getHighestDuplicates(diceValuesArr);
    detectFullHouse(diceValuesArr);
    checkForStraights(diceValuesArr);
  }
});

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

this will run and exit immediately because of the return so it will never get to the next if block.

You want to update the last button all the time for this function as well. (I know it is not what it said in the instructions but that’s a bug and they haven’t fixed it yet)

Also, you want to continue on to check if there is a small straight too.

By updating the last button, do you mean put it in each if statement? I’ve tried that, and it doesn’t work.

Also, I updated the code to include the small straight in the first if block and removed the returns. It still isn’t working.

the last button should always get updated when the function is called, so you can put that code outside the if statement completely.

make sure to test your code by playing the game too to make sure it works as you’ve been expecting.
(post the updated code if you still need help with it)

I got it! I had to change the condition of the first if block and put it in its own for loop. It’s working now.

Thanks again.

1 Like