Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

It keeps giving me the error below and i don’t undersatnd why, since if i try the code, it works fine:
2. If a small straight is rolled, your checkForStraights function should enable the fourth radio button, set the value to 30, update the displayed text to , score = 30 and leave the fifth radio button disabled.

Your code so far

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

/* file: styles.css */

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

const checkForStraights = (arr) => {
  const counts = arr.sort((a,b) => a-b).join('');
  
  if(counts === '12345' || counts === '23456'){
    updateRadioOption(4, 40);
  }
  if (counts.slice(0,4) === '1234' || counts.slice(0,4) === '2345'){
    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);
  }
});

// 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/133.0.0.0 Safari/537.36 OPR/118.0.0.0

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

What happens if you roll 5,4,3,2,3?

It probably wouldn’t work since i’ve discovered that the code that i posted doesn’t like arrays with duplicates (unfortunally, i kept going after posting, so the code changed with the one below:

const checkForStraights = (arr) => {
  const set1 = new Set(arr);
  const counts = [...set1].sort().join('');
  
  if(counts === '12345' || counts === '23456'){
    updateRadioOption(3, 30);
    updateRadioOption(4, 40);
  }
  if (counts === '1234' || counts === '2345'){
    updateRadioOption(3, 30);
  } 
    updateRadioOption(5, 0);
}

But even with whis one, i’m still getting the same error. I used the Set to clean the array from duplicates.

Using Set was a good choice. Do you think there might be an issue with hard-coding the possible sequences?

I’m not sure. As far i can tell (for now), the hard-coded solution should check all possibilities. I’ll keep trying and if it keeps not working, i’ll try a different approach, even if i think this one should be fine.

what if you roll 1,2,3,4,6, what does your code do?

It goes directly for the updateRadioOption(5, 0);. I’ve tried to put a splice in the second if to cut the last index but it keeps going for the last updateRadioOptions.

Would you post your latest code please?

const checkForStraights = (arr) => {
  const set1 = new Set(arr);
  const counts = [...set1].sort().join('');
  
  if(counts === '12345' || counts === '23456'){
    updateRadioOption(3, 30);
    updateRadioOption(4, 40);
  }
  if (counts.split(0,4) === '1234' || counts.split(0,4) === '2345'){
    updateRadioOption(3, 30);
  } 
    updateRadioOption(5, 0);
}

Look carefully at this code. Is that what you intended?

const checkForStraights = (arr) => {
  const set1 = new Set(arr);
  const counts = [...set1].sort().join('');
  
  if(counts === '12345' || counts === '23456'){
    updateRadioOption(3, 30);
    updateRadioOption(4, 40);
  }
  if (counts.slice(0,4) === '1234' || counts.slice(0,4) === '2345'){
    updateRadioOption(3, 30);
  } 
    updateRadioOption(5, 0);
}

I’ve confused slice() with split() :sweat_smile:.
But still not working.

:slight_smile: It happens. Your code looks good. There’s just one more thing you need to consider with this approach. Have you satisfied all possible combinations in your hard-coded sequences?

Use console.log() to check what inputs are being sent to your function by the automated tests and how your function behaves.

what if… 1, 3, 4, 5, 6?

Yeah. Good catch. So much for those hard-coded sequences…

Update: Actually, it could still be done, yes, the tests do pass with the hard-coded approach as long as long as all possible sequences and array patterns are considered.

All 3,4,5,6 cases wouldn’t work, since the code doesn’t take into consideration that pattern. So yeah, at this point i’m just missing what patterns are not working with the code.

You will also need to handle where in the array the sequence could occur, right?

I’ve finally completed it, even if i’ve done it in an ugly way. Here’s the code and thanks for the help! :laughing:

const checkForStraights = (arr) => {
  const set1 = new Set(arr);
  const counts = [...set1].sort().join('');
  const smallRegex = /1234|2345|3456/;
  const largeRegex = /12345|23456/;

  if(largeRegex.test(counts)){
    updateRadioOption(3, 30);
    updateRadioOption(4, 40);
  }
  if (smallRegex.test(counts)){
    updateRadioOption(3, 30);
  } 
    updateRadioOption(5, 0);
}