Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

I feel like I’m close here, but the tests are failing, and it keeps saying I need to update the displayed text with score = 30 or score = 40. I looked at my functions to check for a full house, 3 of a kind, or 4 of a kind, and they don’t look much different than the checkForStraights function, so I’m confused as to what I’m doing wrong.

Your code so far

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

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

const checkForStraights = (arr) => {
  const sortedArr = arr.sort((a, b) => a - b);
  if (sortedArr === [1, 2, 3, 4] || sortedArr === [2, 3, 4, 5] || sortedArr === [3, 4, 5, 6] || sortedArr === [1, 2, 3, 4, 5] || sortedArr === [2, 3, 4, 5, 6]) {
    if (sortedArr === [1, 2, 3, 4] || sortedArr === [2, 3, 4, 5] || sortedArr === [3, 4, 5, 6]) {
      updateRadioOption(3, 30);
    }
    if (sortedArr === [1, 2, 3, 4, 5] || sortedArr === [2, 3, 4, 5, 6]) {
      updateRadioOption(4, 40);
    }
  } 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/130.0.0.0 Safari/537.36 Edg/130.0.0.0

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

Your primary problem is that you can’t compare arrays using equality operators.

I found an fCC article that seems to do a pretty good job of explaining what you need to do: Comparing Arrays in JavaScript – How to Compare 2 Arrays in JS

There’s a second problem, but it’s not really breaking anything. The roll of the die can’t be a 6. So, you don’t need to check for arrays that contain 6. This line:

const randomDice = Math.floor(Math.random() * 6) + 1

… generates a number from 0 to 4, then adds 1. So, the result can be 1, 2, 3, 4, or 5

Wait, I thought the dice could have numbers 1 through 6, not 1 through 5. I know there are only 5 dice, but I thought they could have numbers 1 through 6.

I’m sorry. You’re right. It can be from 1 to 6. I read it wrong.

The equality operators is the only actual problem with your code.

I changed the checkForStraights function to say:

const checkForStraights = (arr) => {
  const sortedArr = arr.sort((a, b) => a - b);
  const sortedArrString = sortedArr.toString();
  if (sortedArrString === "1,2,3,4" || sortedArrString === "2,3,4,5" || sortedArrString === "3,4,5,6" || sortedArrString === "1,2,3,4,5" || sortedArrString === "2,3,4,5,6") {
    if (sortedArrString === "1,2,3,4" || sortedArrString === "2,3,4,5" || sortedArrString === "3,4,5,6") {
      updateRadioOption(3, 30);
    }
    if (sortedArrString === "1,2,3,4,5" || sortedArrString === "2,3,4,5,6") {
      updateRadioOption(4, 40);
    }
  } else {
    updateRadioOption(5, 0);
  }
}

It’s still failing though.

I just realized I probably need to remove duplicates from the array before I compare it.

1 Like

Yeah, that was the problem. Just passed the tests!