Review algorithmic thinking by building a dice game - Step 7

the code works but i am still mising 3 things, 4, 6, and 7
i got the score when i roll, but what to do for my code to pass?

  1. When the array has three of the same number, your getHighestDuplicates function should update the Three of a Kind radio option with , score = and the total sum of the dice.
const getHighestDuplicates = () => {
  const diceCounts = {};
  let totalSum = 0;

  // Count occurrences of each die value
  diceValuesArr.forEach(value => {
    diceCounts[value] = (diceCounts[value] || 0) + 1;
    totalSum += value;
  });

  let fourOfAKind = false;
  let threeOfAKind = false;
  let value = 0;
  let threeOfAKindSum = 0;

  // Check for Four of a Kind or Three of a Kind
  for (const [key, count] of Object.entries(diceCounts)) {
    const dieValue = parseInt(key);

    if (count >= 4) {
      fourOfAKind = true;
      value = dieValue;
      totalSum = value * 4; 
      break; 
    } else if (count === 3) {
      threeOfAKind = true;
      value = dieValue;
      threeOfAKindSum = value * 3;
    }
  }

  if (fourOfAKind) {
    updateRadioOption(1, totalSum); 
  } else if (threeOfAKind) {
    updateRadioOption(0, threeOfAKindSum);
  } else {
    updateRadioOption(5, 0); 
  }
};
  
  
rollDiceBtn.addEventListener("click", () => {
  if (rolls === 3) {
    alert("You have made three rolls this round. Please select a score.");
  } else {
    rolls++;
    rollDice();
    updateStats();
    getHighestDuplicates();
  }
});

Can you post a link the this challenge?

It looks like your in the right direction but I believe you need arr as a parameter and then go from there with the required logic.

1 Like
  1. Pass the function the array and use a parameter for it instead of the top-level array variable.

  2. You already have the total sum calculated in the forEach, pass that value (totalSum) to the updateRadioOption function.

  3. Four of a kind includes three of a kind, so you should update both radio buttons/scores.

When the array has four of the same number, your getHighestDuplicates function should also update the Three of a Kind radio option

1 Like

thank you very much for your help,
i 've solved it :slight_smile: