Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

  1. If a small straight is rolled, your checkForStraights function should enable the fourth radio button, set the value to 30, and update the displayed text to , score = 30.
  2. If a large straight is rolled, your checkForStraights function should enable the fifth radio button, set the value to 40, and update the displayed text to , score = 40.
  3. 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

I have tested this manually by passing a straights array and non straights. It’s able detect both small and large straights and call the updateRadioOption() with the right arguments. I’m having trouble figuring out what’s wrong with the code.

Your code so far

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

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

const checkForStraights = (arr) => {
  const tally = {};
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] - arr[i - 1] === 1) {
      tally["true"] = tally["true"] ? tally["true"] + 1 : (tally["true"] = 1);
    }
  }

  if (tally["true"] === 4) {
    updateRadioOption(4, 40);
  }
  if (tally["true"] >= 3) {
    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/129.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

first thing is you will need to move this update out of the else. You want this to happen all the time when this function is called.

[quote=“Arlen, post:1, topic:713961”]

Next, read the exercise again. They said that a large straight is

A large straight is when all five dice have consecutive values in any order

5 dice, not 4

and small straight is 4 dice, not 3
also a large straight by definition, includes a small one. (so 2 radio buttons will need to be updated in the case of 5 dice forming a straight)

Edit: I think your code will work to set 2 radio buttons on for a large straight given the >= comparison

arr[1] - arr[0] === 1

Wouldn’t you have to sort the array first for this to work?


As said, if you get a large straight, you have to enable the radio buttons for both small and large.

Aside, you don’t need the assignment for falsy in the ternary condition because you already have an assignment before the ternary. The conditions just need to evaluate to a value (i.e. 1 for the falsy condition).

1, 3 , 2 , 4, 5 will work if I sorted it. Should that work ? I’m starting to doubt my approach. I feel as if I overcomplicated a simple problem.

const checkForStraights = (arr) => {
  const ascendingTally = {};
  const descendingTally = {};
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] - arr[i - 1] === 1) {
      ascendingTally["true"] = ascendingTally["true"] ? ascendingTally["true"] + 1 : (ascendingTally["true"] = 1);
    }
    if(arr[i]  - arr[i - 1] ===  -1) {
      descendingTally["true"] = descendingTally["true"] ? descendingTally["true"] + 1 : (descendingTally["true"] = 1);
    }
  }

  if (ascendingTally["true"] === 4 || descendingTally["true"] === 4) {
    console.log("large Straight");
    updateRadioOption(4, 40);
  }
  if (ascendingTally["true"] >= 3 || descendingTally["true"] >= 3) {
    console.log("smal straight");
    updateRadioOption(3, 30);
  } 
  updateRadioOption(5, 0);
};

All I had to do to make your initial code pass was to sort the array and make sure to update both radio buttons in the first condition.

ascendingTally["true"] === 4

the for loop above subtracts a value from an array to the one before it and make sure that it’s equal to 1. In a five digit value there are only up to four comparisons. So if there are three true it’s small straight and if there’s four it’s a large straight. I’m probably wrong thought. I’m not really sure .

That’s fine, I didn’t check your algorithm closely as I was just going on first impressions. You definitely should confirm your code works with something like [5,6,4,2,3]

thanks! it did work. I’m having trouble understanding why though. Is [1, 3 , 2, 4 , 5] a straight?

Yes. The order of the dice doesn’t matter. Just like how a pair in poker isn’t dependent on how the cards are ordered.

Although, it does say “consecutive values”, so I’m not sure if it really means that or not?

Edit: If this was a real life dice game, how would you roll the dice so they not only had a sequence but landed in the correct order? I mean, that is pretty much impossible.

does your code works for [1,2,2,3,4,5]?

It does now that I’ve sorted the array.

So I was solving a different a different problem. It said consecutive so I thought its descending or ascending. I didn’t know the order shouldn’t matter at all. Thank you for explaining.

How would there be 6 dice? (die, dices, whatever).


It is a bit misleading. But I’m not sure how to word it. It is “sequential in any order”, I guess?

Even if the order makes sense for a computer game, it doesn’t make sense if it is emulating a real dice game.

I’m no statistician, but I’m pretty sure the chance of you rolling five consecutive numbers and them also landing on the table in order is pretty low.

1 Like

ops, well, then [1, 2, 2, 3, 4]