Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

function checkForStraights (arr) {
const sortedArr = arr.sort().map((a,b) => a+b);
let counted = sortedArr.filter((val)=> val === 1).length;

let counts = {};
sortedArr.forEach((num) => {
counts[num] = (counts[num] || 0) + 1;
});
let consecutiveObject = […Object.values(counts)];

if(consecutiveObject.includes(5)){
updateRadioOption(4, 40);
}else if(consecutiveObject.includes(4)){
updateRadioOption(3, 30);
}else{
updateRadioOption(5, 0);
}

rollDiceBtn.add

Your code so far

hola , tengo problemas para completar este codigo ya probe de muchas opciones pero no pasa nada.

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

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

function checkForStraights  (arr)  {
  const sortedArr =  arr.sort().map((a,b) => a+b);
  let counted = sortedArr.filter((val)=> val === 1).length;

  let counts = {};
  sortedArr.forEach((num) => {
    counts[num] = (counts[num] || 0) + 1;
  });
  let consecutiveObject = [...Object.values(counts)];

  if(consecutiveObject.includes(5)){
    updateRadioOption(4, 40);
  }else if(consecutiveObject.includes(4)){
    updateRadioOption(3, 30);
  }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

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

Log out counts and consecutiveObject I don’t think your code is doing what you think it is.

A straight is a list of numbers, like 1234 or 12345. Counting duplicate numbers do not really help you to figure out if it is a straight or not (they just shouldn’t be counted).

The simplest solution is to check against hard-coded possible straight values (could be strings, or an array of strings) after you have removed any duplicate numbers from the dice roll.


Not sure what you are trying to do here?

const sortedArr = arr.sort().map((a, b) => a + b);
let counted = sortedArr.filter((val) => val === 1).length;

Also, you didn’t close the function.

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);

}
});
tengo que completar este codigo pero nada de lo que haga funciona.

That is just the click handler code. Your logic inside checkForStraights is not correct.

Also, checkForStraights is the function you didn’t close.

si es esa como tengo que hacer para completar el codigo.