Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

I have tried a couple of different approaches to this, and neither seems to pass the test even though, on the surface at least, they give correct results. Any insight and help would be greatly appreciated. What am I missing here?

Your code so far

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

/* file: styles.css */

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

const checkForStraights = (arr) => {
  let previousNum;
  let maxConsecutiveCount = 0;
  let currConsecutiveCount = 0;

  arr.forEach((el, index) => {
    if (index === 0) {
      previousNum = el;
    } else {
      if (Math.abs(el - previousNum) === 1) {
        previousNum = el;
        currConsecutiveCount++;
        if (currConsecutiveCount > maxConsecutiveCount) {
          maxConsecutiveCount = currConsecutiveCount;
        }
      } else {
        previousNum = el;
        currConsecutiveCount = 0;
      }
    }
  });

  if(maxConsecutiveCount === 4) {
    updateRadioOption(4, 40);
    updateRadioOption(3, 30);
  } else if (maxConsecutiveCount === 3) {
    updateRadioOption(3, 30);
  } else {
    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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

So i finally got it. I must say that the task objective is a little bit confusing. When reading it I got the impression that a straight can only be scored if the numbers are in order (for example 12345 is a large straight, but 13254 is not a large straight). That was not the case. The order does not matter.

1 Like

You saved me. I spent like two hours trying to understand what was the problem with my code!