Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

Please Help!

I am not passing 2. If a small straight is rolled…

I did a test run (below my checkForStraights function), the 5th button is off… not really sure what is going on…

Thanks!

Your code so far

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

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

const checkForStraights = (arr) => {
  // counts number repetitions
  let counts = {};
  for (let num of arr) {
    counts[num] = (counts[num] || 0) + 1;
  } 
  
  //change object to array
  let countToArr = Object.entries(counts);
  
  //takes the first element of child arrays and change it to a number
  const elArr = countToArr.map(el => parseInt(el[0])); 

  //changes elArr array to string
  const arrToStr = elArr.join('');

  if(arrToStr === "1234" || arrToStr === "2345" || arrToStr === "3456") {
    updateRadioOption (3, 30);
  } else if (arrToStr === "12345" || arrToStr === "23456") {
    updateRadioOption (3, 30);
    updateRadioOption (4, 40);
  } else {
    updateRadioOption (5, 0);
  }
}

const x = [2,3,4,5,2];
checkForStraights(x)

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; rv:136.0) Gecko/20100101 Firefox/136.0

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

Hi there, welcome to the community! :blush:

You’re making great progress! However, I noticed a few issues in your code that might cause unexpected behavior:

Issues in Your Code:

  1. Sorting & Duplicates – Your function doesn’t sort the array or remove duplicates before checking for straights. This can lead to incorrect results if numbers are out of order or repeated.

  2. Incorrect Sequence Checking – The way you’re comparing the number sequences directly as strings might not work correctly if the order isn’t consistent.

  3. Redundant Conditions – You’re performing multiple checks that could be optimized, making the function cleaner and more efficient.

How to Fix It:

  • Sort the array to ensure numbers are in the correct order.
  • Remove duplicates so repeated numbers don’t interfere with the sequence check.
  • Use substring matching instead of direct comparison to detect valid straights more reliably.

You’re almost there! Try making these changes, and your function will work much better. Let me know if you need any help!