Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

I’ve debugged and tested the function and everything seems to work just fine. Yes, I used a string search for possibilities instead of using math, but it all works the same here. Despite my testing, I still get an error saying that the function doesn’t properly react to a small straight.

I’ve also included some comments to label what code does what. I have no idea what could be wrong. What am I missing here?

Your code so far

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

/* file: styles.css */

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

const checkForStraights = (arr) => {
  //Joins the array into a string for easy searching
  const strArr = arr.join('');
  //Checks for large straight and updates large + small straight buttons
  if(strArr.includes("12345") || strArr.includes("23456")) {
    updateRadioOption(4, 40);
    updateRadioOption(3, 30);
 
    return;
  }
  //Checks for small straight and updates small straight button
  if(strArr.includes("1234") || strArr.includes("2345") || strArr.includes("3456")) {
    updateRadioOption(3, 30);
    return;
  }
    //Default case
    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; rv:129.0) Gecko/20100101 Firefox/129.0

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

Take a look how the function behaves when it’s used during the roll button press. For example - add console.log to log in console arr or strArr.

When I log arr or strArr in the console, it works as expected.

const checkForStraights = (arr) => {
  //Joins the array into a string for easy searching
  const strArr = arr.join('');
  //Checks for large straight and updates large + small straight buttons
  if(strArr.includes("12345") || strArr.includes("23456")) {
    updateRadioOption(4, 40);
    updateRadioOption(3, 30);
    console.log(arr);
    console.log(strArr, "large straight");
    return;
  }
  //Checks for small straight and updates small straight button
  if(strArr.includes("1234") || strArr.includes("2345") || strArr.includes("3456")) {
    updateRadioOption(3, 30);
    console.log(arr);
    console.log(strArr,"short straight");
    return;
  }
    //Default case
    updateRadioOption(5, 0);
    console.log(arr);
    console.log(strArr, ", no straight");
}
rollDiceBtn.addEventListener("click", () => {
    rolls++;
    resetRadioOptions();
    rollDice();
    updateStats();
    getHighestDuplicates(diceValuesArr);
    detectFullHouse(diceValuesArr);
    checkForStraights(diceValuesArr);
});

With an excerpt of the console messages:

[ 1, 3, 1, 6, 6 ]
13166 , no straight
[ 4, 2, 6, 1, 4 ]
42614 , no straight
[ 5, 3, 4, 5, 6 ]
53456 short straight
[ 2, 6, 1, 2, 2 ]
26122 , no straight

What should I be noticing in particular?

That was a bit lucky draw. Here’s couple of my tries:

[ 4, 3, 2, 4, 6 ]
43246 , no straight
[ 1, 3, 2, 1, 4 ]
13214 , no straight
[ 4, 2, 5, 3, 3 ]
42533 , no straight
[ 6, 5, 3, 4, 1 ]
65341 , no straight

I see, the error is human. I forgot that you can roll a straight without the specific dice being in order. Thank you for pointing this out!

1 Like