Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

I have tried most edge cases with my checkForStraight function. But i keep failing the second test. I dont know what I am doing wrong.

Your code so far

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

/* file: styles.css */

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

const checkForStraights = (array) => {
  const reduced = [...new Set(array).values()].toSorted((a, b) => a - b);
  const consq = reduced.every((num, i) => i === 0 ? true : (num - reduced[i-1]) === 1);

  if (reduced.length === 4 && consq) {
    updateRadioOption(3, 30);
  }
  
  if (reduced.length === 5 && consq) {
    updateRadioOption(4, 40);
    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);
  }
});

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Safari/605.1.15

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

check what your function does for [1,2,3,4,6]

1 Like

I have sorted out that problem and my code seems to be working for larger test cases. But it still refuses to pass the second test. Have I interepreted the prompt wrong?

This is my current function.

const checkForStraights = (array) => {
  const reduced = [...new Set(array).values()].toSorted((a, b) => a - b);
  const filtered = reduced.filter((num, i) => i === 0 ? true : reduced[i-1] === num - 1);
  const consec = filtered.every((num, i) => i === 0 ? true : num - 1 === filtered[i - 1]);

  if (filtered.length === 4 && consec) {
    console.log(filtered);
    updateRadioOption(3, 30);
  } else if (filtered.length === 5 && consec) {
    console.log(filtered);
    updateRadioOption(4, 40);
    updateRadioOption(3, 30);
  }

  updateRadioOption(5, 0);
};

The issue remains. Your code does not handle [3,4,1,5,6].

1 Like

Thank you for this, I managed to fix it.