Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

I sort the incoming array from small to large, and then convert it into a string to match the rules. I passed in several arrays for testing and there was no problem. I read the code of the console: (4. If a large straight is rolled, your checkForStraights function should also enable the fourth radio button, set the value to 30, and update the displayed text to , score = 30.) This is a contradiction and I’m confused by it.

Your code so far

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

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

const checkForStraights =(array)=>{ 
  for(let i=0;i<array.length;i++){    
    for(let j=i+1;j<array.length;j++){
      if(array[j]<array[i]){
        let temp = array[i];
        array[i] = array[j];
        array[j] = temp;
      }
    }
  }
  const regex4 = /1234|2345/g;
  const regex5 = /12345/g;
  const newstr = array.join("");
  if(regex5.test(newstr)){
    updateRadioOption(3, 30);
    updateRadioOption(4, 40);
    
  }else if(regex4.test(newstr)){
    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
/* 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/127.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

it means that if a large straight is rolled, you should also update the small straight button since a large includes a small.

also don’t put this line of code in the else.
Just run it all the time when this function is called. (in spite of what the step may have said)

never use the g flag with test unless you have researched it and know you need it

But I’ve made sure it’s enabled.
if(regex5.test(newstr)){
updateRadioOption(3, 30);
updateRadioOption(4, 40);

}

1 Like

Thanks for the heads up, I guess this shouldn’t be a problem since it’s already ensured that the length of the array is always 5, it should be dispensable here

the g flag does not have an issue with the length of the string, just don’t use it

explanation if you want to investigate:

1 Like

this regex will fail to catch a straight that include the number 6

1 Like

I always thought there wasn’t much difference between the two, thank you so much for letting me know!

…I must be cursed by the devil to think that the sieve only has 5 digits, thank you for your patience!

1 Like