Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

i can’t get to pass the second option which say “If a small straight is rolled, your checkForStraights function should enable the fourth radio button, set the value to 30, update the displayed text to , score = 30 and leave the fifth radio button disabled.” I don’t know what I’m missing. i need help on that thank you

Your code so far

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

/* file: styles.css */

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

const checkForStraights = (arr) => {
  const cleanArr = arr.sort().filter((el, i, a) => i === a.indexOf(el));

  const hasFourOrFiveConsecutive = (arr) => {
    let n = arr.length;

    let mini = Math.min(...arr);
    let maxi = Math.max(...arr);

    if (maxi - mini + 1 !== n) return false;

    for (let i = 0; i < n; i++) {
      let val = Math.abs(arr[i]);

      if (arr[val - mini] < 0) {
        return false;
      }
      
      arr[val - mini] *= -1;
    }
    return true;
  }

  if (hasFourOrFiveConsecutive(cleanArr) && cleanArr.length === 5) {
    updateRadioOption(4, 40);
    updateRadioOption(3, 30);
  } else if (hasFourOrFiveConsecutive(cleanArr) && cleanArr.length === 4) {
    updateRadioOption(3, 30);
  }
  const test = [2,4,3,1];
  console.log(hasFourOrFiveConsecutive(test))
  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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

There is a demo of your app to the side. I recommend playing it. But in case RNG luck isn’t on your side, I have a screenshot of your app.

I think you might be able to spot the bug.

1 Like

i created another function. This is what i have now
const checkForStraights = (arr) => {
const cleanArr = arr.sort().filter((el, i, a) => i === a.indexOf(el));

const hasFiveConsecutive = (arr) => {
let n = arr.length;

let mini = Math.min(...arr);
let maxi = Math.max(...arr);

if (maxi - mini + 1 !== n) return false;

for (let i = 0; i < n; i++) {
  let val = Math.abs(arr[i]);

  if (arr[val - mini] < 0) {
    return false;
  }
  
  arr[val - mini] *= -1;
}
return true;

}

const hasFourConsecutive = (arr) => {
arr.sort((a, b) => a - b);

const uniqueArr = [...new Set(arr)];

for (let i = 0; i <= uniqueArr.length - 4; i++) {
  if (
    uniqueArr[i + 1] === uniqueArr[i] + 1 &&
    uniqueArr[i + 2] === uniqueArr[i] + 2 &&
    uniqueArr[i + 3] === uniqueArr[i] + 3
    ) {
      return true;
    }
}

return false;

}

if (hasFiveConsecutive(cleanArr) && cleanArr.length === 5) {
updateRadioOption(4, 40);
updateRadioOption(3, 30);
} else if (hasFourConsecutive(cleanArr)) {
updateRadioOption(3, 30);
}
updateRadioOption(5, 0);
}

It worked out. Thank you