Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

My code is working when I manually pass in checkForStraights([1, 2, 3, 4, 5]). It tells me if a large straight is rolled that it should also enable the 4th radio button with a score = 30, but that’s what is happening. Small is getting 30, large is getting 40, and none is getting 0. All final 3 options are enabled.

Your code so far

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

/* file: styles.css */

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

const checkForStraights = (arr) => {
  const largeStraightRules = [
    arr.includes(1),
    arr.includes(2),
    arr.includes(3),
    arr.includes(4),
    arr.includes(5)
  ];

  const smallStraightRulesLow = [
    arr.includes(1),
    arr.includes(2),
    arr.includes(3),
    arr.includes(4)
  ];

  const smallStraightRulesHigh = [
    arr.includes(2),
    arr.includes(3),
    arr.includes(4),
    arr.includes(5)
  ];

  if (largeStraightRules.every(rule => rule)) {
    updateRadioOption(4, 40);
  } 
  
  if (smallStraightRulesLow.every(rule => rule) || smallStraightRulesHigh.every(rule => rule)) {
    updateRadioOption(3, 30);
  }

  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);
    checkForStraights([1, 2, 3, 4, 5]);
  }
});

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

I just realized the dice goes up to 6 and not 5.

This code works:

const checkForStraights = (arr) => {
  const largeStraightRulesLow = [
    arr.includes(1),
    arr.includes(2),
    arr.includes(3),
    arr.includes(4),
    arr.includes(5)
  ];

  const largeStraightRulesHigh = [
    arr.includes(2),
    arr.includes(3),
    arr.includes(4),
    arr.includes(5),
    arr.includes(6)
  ];

  const smallStraightRulesLow = [
    arr.includes(1),
    arr.includes(2),
    arr.includes(3),
    arr.includes(4)
  ];

  const smallStraightRulesMid = [
    arr.includes(2),
    arr.includes(3),
    arr.includes(4),
    arr.includes(5)
  ];

  const smallStraightRulesHigh = [
    arr.includes(3),
    arr.includes(4),
    arr.includes(5),
    arr.includes(6)
  ];

  if (largeStraightRulesLow.every(rule => rule) || largeStraightRulesHigh.every(rule => rule)) {
    updateRadioOption(4, 40);
  }
  
  if (smallStraightRulesLow.every(rule => rule) || smallStraightRulesMid.every(rule => rule) || smallStraightRulesHigh.every(rule => rule)) {
    updateRadioOption(3, 30);
  }

  updateRadioOption(5, 0);
};

@freefranklin

We have blurred this solution (with [spoiler][/spoiler] tags) so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

1 Like