Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

I’m struggling with the logic for detecting a straight. I started by sorting the array and then used a new Set to filter out duplicates and return unique values. Then I used if logic to look for a delta of 3 between index values for the small straight, and a delta of 4 between the first and last index values for the large straight. Finally I called the updateRadioOption function to update the scores. I’m running out of ideas, can anyone explain where I’m going wrong? Thanks in advance!

Your code so far

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

/* file: styles.css */

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

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

  if(dupsRemoved[3] - dupsRemoved[0] === 3 || dupsRemoved[4] - dupsRemoved[1] === 3) {
    updateRadioOption(4, 30);
  };
  if (dupsRemoved[4] - dupsRemoved[0] === 4) {
    updateRadioOption(5, 40);
    updateRadioOption(4, 30);
  } else {
    updateRadioOption(6, 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/130.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

Hi @maddeebee

To help you troubleshoot, try console logging the dupsRemove array.

Happy coding

Hi, my first approach to this step included a Set, though it removes duplicates, it is also removing one of the five elements available for you to check.
I would suggest simply using a for loop to check your sorted array keeping the duplicate element as the array has a fixed length you can deal with the duplicate element with a simple condition check.
Be aware though the index that you select to start looping your array and the stop condition affect the comparisons you can make to verify the presence of a straight so grab a piece of paper and identify what that difference is and what value you should be using to start looping the array.
Try it out it out using a simple approach like [1,2,3,4,4] this helped me to see what i was doing wrong.
Hope this can help you out a bit do not give up!!