Learn Intermediate Algorithmic Thinking by Building a Dice Game - Step 87

The Dice game itself doesn’t work.

I answered the questions, and completed the code, but if I roll, say “1, 2, 3, 4, 6” it doesn’t give me the “Small Straight” option. Probably just an oversight, and easy to update with a “forEach” loop rather than just an “includes”

const checkForStraights = (arr) => {
  const sortedNumbersArr = arr.sort((a, b) => a - b);
  const uniqueNumbersArr = [...new Set(sortedNumbersArr)];
  const uniqueNumbersStr = uniqueNumbersArr.join("");

  const smallStraightsArr = ["1234", "2345", "3456"];
  const largeStraightsArr = ["12345", "23456"];

//***issue is here***
  if (smallStraightsArr.includes(uniqueNumbersStr)) {
    updateRadioOption(3, 30);
  }

  if (largeStraightsArr.includes(uniqueNumbersStr)) { 
    updateRadioOption(4, 40);
  }

  updateRadioOption(5, 0);
};

Learn Intermediate Algorithmic Thinking by Building a Dice Game - Step 87

I can’t reproduce your issue. includes should work correctly - includes does the same thing as a for loop checking to see if the array includes the value.

Some straights can be missed, you are right.

This is caused by the smallStraightsArr contain only cases with 4 numbers:

  const sortedNumbersArr = arr.sort((a, b) => a - b);
  const uniqueNumbersArr = [...new Set(sortedNumbersArr)];
  const uniqueNumbersStr = uniqueNumbersArr.join("");

  const smallStraightsArr = ["1234", "2345", "3456"];

This will work when rolled die have one repeat, ie. 1, 2, 2, 3, 4 - Set handles the duplicated 2.
On the other hand when all die have unique numbers, ie. 1, 2, 3, 4, 6, the uniqueNumbersStr will be "12346" and it will not be detected.

1 Like

To reproduce just manually change the “rollDice” function from an empty array to [1,2,3,4,6].

What it’s trying to do is see if smallStraightArr - [“1234”, “2345”, “3456”] INCLUDES “12346”, which it doesn’t

Ah, I see the issue now. But no, its not something that’s fixed by ‘just’ using a forEach loop.

Why not?

smallStraightArr.forEach(straight => {
  if(uniqueNumberStr.includes(straight)) {
    updateRadioOption(3, 30);
  }
}

Ah, that’s what you meant by "easy to update with a forEach. I’d use this:

  if (largeStraightsArr.includes(uniqueNumbersStr)) {
    updateRadioOption(4, 40);
  }

  if (smallStraightsArr.some(straight => uniqueNumbersStr.includes(straight))) {
    updateRadioOption(3, 30);
  }

Admittedly that is better than mine haha. Similar idea though.

But either way not a huge leap of logic if/when the lesson is next updated

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like