Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

Really struggling with this one. Been on it for hours and this is what I’ve got. It seems to me that it isn’t properly running the first if. In my mind if my array is [1,2,3,4,5] then it should work. For example, if arr[i] = 1 then arr[i + 1] would equal 2. 2-1 should equal arr[i]. Am I wrong in thinking this? I’l love some help.

Your code so far

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

/* file: styles.css */

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

const checkForStraights = (arr) => {

  for (let i = 0; i < arr.length - 1; i++) {
    if (arr[i] === arr[i + 1] - 1) {
      updateRadioOption(4, 40);
      updateRadioOption(3, 30);
    } else if ([...new Set(arr)].length === 4 && arr[i] === arr[i + 1] - 1) {
     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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

What if it’s different array? Ie. [5, 3, 4, 1, 2] or [1, 3, 2, 2, 3]?

Great point. I put arr = arr.sort() at the top. Now it is just telling me I need my small straight. I calculated that but using the new set method. This would get rid of dupes making the array 4 numbers instead of 5. Would that not be correct?

If I understand correctly, for array like [1, 3, 1, 5, 4], duplicates would be removed and it would be sorted? Resulting in array: [1, 3, 4, 5].

Yep. That is why i put 2 items in the condition, check that its length is 4 and the same sequential if statement

Hmm, I replaced the call in rollDiceBtn callback with checkForStraights([1, 3, 1, 5, 4]), to simulate this case when roll button is clicked. Both option for the Small straight and Large straight got enabled.

So what you’re saying is my code is working on your end and not mine?

Well, no, [1, 3, 1, 5, 4] is not straight but it got marked as both small and large straight.

Is my if statement wrong? Am I thinking about it the wrong way?

Before I added arr = arr.sort() I had the error for 2-6. With my current changes I am only getting the alert: 2. 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

.

I changed the order of the if statements and I passed. However, the game doesn’t actually work as intended. Any ideas?

What’s your updated code?

const checkForStraights = (arr) => {
  arr = arr.sort();
  const noDupes = [...new Set(arr)]
  for (let i = 0; i < arr.length - 1; i++) {
    if (noDupes.length === 4 && arr[i] === arr[i + 1] - 1) {
     updateRadioOption(3, 30);
    } else if (arr[i] === arr[i + 1] - 1) {
      updateRadioOption(4, 40);
      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);
  }
});

Try following step-by-step what happens inside of the function. Taking as the example mentioned [1, 3, 1, 5, 4] array. It’s sorted to [1, 1, 3, 4, 5], noDupes has [1, 3, 4, 5]. What changes when arr is iterated over?

The length of the arr?

I’m confused as to what you are asking. The loop runs starting at 0, runs as long as i < the length of the arr and goes on to the next i. That would be my answer if you are talking literal.

Specifically focusing on this part:

  for (let i = 0; i < arr.length - 1; i++) {
    if (noDupes.length === 4 && arr[i] === arr[i + 1] - 1) {
     updateRadioOption(3, 30);
    } else if (arr[i] === arr[i + 1] - 1) {
      updateRadioOption(4, 40);
      updateRadioOption(3, 30);
  } else {
    updateRadioOption(5, 0);
  }
}

First i will be 0, therefore arr[i] is 1 and arr[i + 1] also 1. Length of noDupes is 4, but arr[i] !== arr[i + 1] - 1, so only the 5th option is updated. What happens for further i values?

I would assume that the loop continues to run through but since arr[i} !== arr[i + 1] -1 in this case it would not be true meaning that it should not run the code block

But ie. for i == 2:

  • arr[2] is 3
  • arr[i + 1]arr[3] is 4
  • arr[2] === arr[3] - 1
  • noDupes.length is 4
  • so 3rd option gets enabled…?

Let’s reword this I’m getting lost. I assume you are talking for the array of [1,1,3,4,5]

arr[0] = 1
arr[0+1] = arr[1] which is 1. Then we subtract that and it doesn’t equal.
Are you suggesting that because that the latter part of the array is true, it is saying the if sentence is true?