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