Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

Good afternoon. push me in which direction I should look. I try different ways, but nothing works.

For the last portion of the game, you will need to create an algorithm that checks for the presence of a straight. A small straight is when four of the dice have consecutive values in any order (Ex. in a roll of 41423, we have 1234) resulting in a score of 30 points. A large straight is when all five dice have consecutive values in any order (Ex. in a roll of 35124, we have 12345) resulting in a score of 40 points.

Declare a checkForStraights function which accepts an array of numbers. If the user gets a large straight, update the fifth radio button with a score of 40. If the user gets a small straight, update the fourth radio button with a score of 30. If the user gets no straight, update the last radio button to display 0.

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

Your code so far

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

/* file: styles.css */

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

const checkForStraights = (array) => {
  const reg = /^((1+)|(12))?(2|3)(3|4)4([1-4|6])?$/;
  const regex = /^12345$/;
  
  if (reg.test(array.sort((a, b) => a - b).join(""))){
    
    updateRadioOption(3, 30);
      
      }
  else if (regex.test(array.sort((a, b) => a - b).join(""))) {
    
    updateRadioOption(4, 40)
    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/132.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

This is the second solution

const checkForStraights = (arr) => {
     const counts = {};

     for (const num of arr) {
       counts[num] = num;
     }
  
     const hasA = Object.values(counts).includes(1);
     const hasB = Object.values(counts).includes(2);
     const hasC = Object.values(counts).includes(3);
     const hasD = Object.values(counts).includes(4);
     const hasE = Object.values(counts).includes(5);
    
     if (hasA && hasB && hasC && hasD) {
       updateRadioOption(3, 30);
     }
     if (hasA && hasB && hasC && hasD && hasE) {
       updateRadioOption(4, 40);
       updateRadioOption(3, 30);
     }
  
     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);
  }
});

I liked your use of regular expressions for this! But the pattern you’re using for the small straight is working for “234” but not for “2345” … see https://regex101.com/

Weird. I’m getting through everything.
And in our own tests with different arrays, everything was fine too.