Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

Hello,
I am not understanding why I cannot pass test 4. If not straight is rolled, your checkForStraights function should not enable the fourth or fifth radio button.

Your code so far

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

/* file: styles.css */

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

const checkForStraights = (arr) => {
  const sorted = new Set([...arr.sort((a, b) => a - b)]);
  let consecutiveCount = 0;
  const sortedArr = [...sorted];
  for (let i=0; i<sortedArr.length; i++) {
    if (sortedArr[i + 1] > sortedArr[i] && sortedArr[i + 1] - sortedArr[i] === 1) {
      consecutiveCount++;
    }
  }
  console.log(consecutiveCount);
  if (consecutiveCount === 4) {
    updateRadioOption(3, 30);
    updateRadioOption(4, 40);
  } else if (consecutiveCount === 3) {
    updateRadioOption(3, 30);
  } 
  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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

This counts how many times one number follows the previous by 1, but it doesn’t reset the count when there’s a break in the sequence.

For example, try this array: [1, 5, 6, 2, 3] → after sorting: [1, 2, 3, 5, 6].

The consecutiveCount will be 3 instead of 2, because it also counts the 5, 6 pair as consecutive even though there’s a break between 3 and 5.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.