Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

HELPP!! Ive been on this for hours const checkForStraights = (arr) => {
const sorted = arr.slice().sort((a, b) => a - b);
const isConsecutive = (numbers) => {
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] !== numbers[i - 1] + 1) {
return false;
}
}
return true;
};
if (sorted.length === 5 && isConsecutive(sorted)) {
updateRadioOption(4, 40);
updateRadioOption(3, 30);
} else {
const smallStraightPatterns = [
[1, 2, 3, 4],

Your code so far

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

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

const checkForStraights = (arr) => {
  const sorted = arr.slice().sort((a, b) => a - b);
  const isConsecutive = (numbers) => {
    for (let i = 1; i < numbers.length; i++) {
      if (numbers[i] !== numbers[i - 1] + 1) {
        return false;
      }
    }
    return true;
  };
  if (sorted.length === 5 && isConsecutive(sorted)) {
    updateRadioOption(4, 40);
    updateRadioOption(3, 30);
  } else {
    const smallStraightPatterns = [
      [1, 2, 3, 4],
      [2, 3, 4, 5],
      [3, 4, 5, 6]
    ];
    let isSmallStraight = false;
    for (const pattern of smallStraightPatterns) {
      if (pattern.every(num => sorted.includes(num))) {
        isSmallStraight = true;
        break;
      }
    }
    if (isSmallStraight) {
      updateRadioOption(3, 30);
    } else {
      updateRadioOption(3, 0);
    }
    updateRadioOption(4, 0);
  }
};
const updateRadioOption = (index, score) => {
  const input = scoreInputs[index];
  const span = scoreSpans[index];

  input.disabled = false;
  input.value = score;

  span.textContent = `, score = ${score}`;
};


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);

  }
});

// User Editable Region
/* file: styles.css */

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

Did you already do this part?

Call your checkForStraights function when the rollDiceBtn is clicked to complete your dice game!

I hadn’t because I believe this isn’t where the main error lied. “Even so I still get the message (diceValuesArr);”

It would help you to debug if you call the function. For eg. You can check the console for errors but not if you never call it.

I did it and it is still not working

So have you tried to debug at all?

You can add console.log statements for eg and check manually to see if what they show is what you expect?

Also watch out here as the logic should update both buttons if there is a small and a big straight.

Edit: you should not set the score to zero for a large straight though.

i have, and i encountered the following error message

frame.ts:269 ReferenceError: resetRadioOptions is not defined
    at eval (eval at <anonymous> (frame-runner.ts:95:35), <anonymous>:1:1)
    at HTMLDocument.<anonymous> (frame-runner.ts:95:35)
    at u (jquery.js:3489:29)
    at c (jquery.js:3557:12)

after that i added

const updateRadioOption = (index, score) => {
  const input = scoreInputs[index];
  const span = scoreSpans[index];

  input.disabled = false;
  input.value = score;

  span.textContent = `, score = ${score}`;
};

but it still displays the same error message

I edited your post because it was hard to read without the code formatted.

that’s fine, thank you!Have you any idea on how to help me?

Check your logic for updating the radio buttons. It doesn’t look correct.

Can you give me any more examples or tips on how to correct it?I have tried changing it but it does not look right.

it’s not the updateRadioOption function that is the issue.

It is how and when you’re calling it.

For eg. you were supposed to update the last option when no straights are found and assign 0 as the score.
The last option is the last one in the scoreInputs array, so you should pass 5 to updateRadioOption if you want to update.