Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

Hello, I believe this code should work because the loop checks if the value + 1 is equal to the value after it (sorted). So after doing that, if count is equal to 5 or more, then it enables the correct radio buttons, otherwise if count is equal to 4 or more, then it enables the small straight and none of the above radio options. After that, if none match the none radio option is enabled. Maybe I have a typo? I have tried the game out a bit, and the straights seem to work. Thank you.

Your code so far

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

/* file: styles.css */

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

const checkForStraights = (arr) => {
  let count = 1;
  const array = arr.sort((a, b) => a-b);
  for(let i = 0; i < array.length-1; i++) {
    if(array[i+1] === array[i]+1) {
      count++;
    } else if(array[i+1] !== array[i]) {
      count = 1;
    };
  };
  if(count >= 5) { 
    updateRadioOption(4, 40);
    updateRadioOption(3, 30);
    updateRadioOption(5, 0);
  } else if(count >= 4) {
    updateRadioOption(3, 30);
    updateRadioOption(5, 0);
  } else {
    updateRadioOption(5, 0);
  }
};
//[1, 2, 3, 4, 5]
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/137.0.0.0 Safari/537.36 Edg/137.0.0.0

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

Welcome to the forum @adyaan752

Regardless, it should always update the last radio button to display a score of 0, with the correct attributes.

See if you can tidy up the code for this requirement.

Happy coding

The code does not pass with the error message:
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.

Here is my code:

const checkForStraights = (arr) => {
  let count = 1;
  const array = arr.sort((a, b) => a-b);
  for(let i = 0; i < array.length-1; i++) {
    if(array[i+1] === array[i]+1) {
      count++;
    } else if(array[i+1] !== array[i]) {
      count = 1;
    };
  };
  if(count >= 5) { 
    updateRadioOption(4, 40);
    updateRadioOption(3, 30);
  } else if(count >= 4) {
    updateRadioOption(3, 30);
  }
  updateRadioOption(5, 0);
};

Console log the count variable and check it is doing what you expect.

what do you think of the array [1,2,3,4,6], is it a straight? and what does your code say about that?

Finally found the issue! Thanks for the example array. I saw the issue was that the count resets even when there is a straight! In order to check for gaps, I added the highest count variable to keep track of the highest count. Thank you!