Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

The dice game seems to be working fine, the large and small straight are updating alright but I get the following error and I’m unable to submit . “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.”

Your code so far

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

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

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

  for (const num of arr) {
    counts[num] = counts[num] ? counts[num] + 1 : 1;
  }

  const keys = Object.keys(counts).join("")

  if (keys.slice(0,5) === '12345' 
   || keys.slice(0,5) === '23456'){
    updateRadioOption(4, 40);
  }

  else if (keys.slice(0,4) === '1234' 
    || keys.slice(0,4) === '2345'
    || keys.slice(0,4) === '3456'
    || keys.slice(1,5) === '1234' 
    || keys.slice(1,5) === '2345' 
    || keys.slice(1,5) === '3456')
    {
    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/109.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

Do not hard code the dice rolls. Manually add the numbers together.

1 Like

Sorry I don’t know what you mean to “manually add the numbers together”.
I would appreciate another tip, thank you.

What I meant was to count occurrence of the numbers.

“42536” is a large straight. Manually coding each arrangement is bound to have you miss a possibility.

That was why I sorted the dice values before checking against the hard-coded values therefore making sure I missed none of the probable variations.

Can you point out where you sorted the dice values?

const keys = Object.keys(counts).join("")

The output comes out sorted.
But on second thought does it work the same way .sort() would?
Could that be the cause of the eror?