Review Algorithmic Thinking by Building a Dice Game - Step 7

Tell us what’s happening:

So i’ve been writing code for almost 6 hours and now mi issue is that getHighthestDuplicates seems to not be returning ,score=0 , but i tried to handle it with updateRadioOption(finalOptionIndex, 0); please help ;-;

Your code so far

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

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

function getHighestDuplicates(dice) {
  let count = {};
  let totalScore = dice.reduce((sum, num) => sum + num, 0); 
  for (let num of dice) {
    count[num] = (count[num] || 0) + 1;
  }
  let hasFourOfAKind = false;
  let hasThreeOfAKind = false;
  for (let key in count) {
    if (count[key] >= 4) {
      hasFourOfAKind = true;
      hasThreeOfAKind = true;
    } else if (count[key] === 3) {
      hasThreeOfAKind = true;
    }
  }
  const threeOfAKindIndex = 0; 
  const fourOfAKindIndex = 1; 
  const finalOptionIndex = 2; 
  if (hasFourOfAKind) {
    updateRadioOption(fourOfAKindIndex, totalScore);
    updateRadioOption(threeOfAKindIndex, totalScore);
  } else if (hasThreeOfAKind) {
    updateRadioOption(threeOfAKindIndex, totalScore);
  } else {   
    updateRadioOption(finalOptionIndex, 0);
  }
}

function getCurrentDiceValues() {
  return Array.from(listOfAllDice).map(die => parseInt(die.textContent));
};
rollDiceBtn.addEventListener("click", () => {
  if (rolls === 3) {
    alert("You have made three rolls this round. Please select a score.");
  } else {
    rolls++;
    rollDice(); 
    let diceValues = getCurrentDiceValues(); 
    getHighestDuplicates(diceValues); 
    updateStats(); 
  }
});

// User Editable Region
/* file: styles.css */

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:134.0) Gecko/20100101 Firefox/134.0

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 7

Here you only need to call the newly created function after updateStats function and add the existing diceValuesArr variable as an argument

rollDiceBtn.addEventListener("click", () => {
  if (rolls === 3) {
    alert("You have made three rolls this round. Please select a score.");
  } else {
    rolls++;
    rollDice();  
    updateStats();
    getHighestDuplicates(diceValuesArr); 
  }
});

Already changed it and still giving me the 2. When the array has less than three of the same number, your

getHighestDuplicates

function should update the final radio option with

, score = 0

.

You shouldn’t create that function. It’s aren’t asked in the challenge instructions.

function getHighestDuplicates(dice) {
  let count = {};
  let totalScore = dice.reduce((sum, num) => sum + num, 0); 
  for (let num of dice) {
    count[num] = (count[num] || 0) + 1;
  }
  let hasFourOfAKind = false;
  let hasThreeOfAKind = false;
  for (let key in count) {
    if (count[key] >= 4) {
      hasFourOfAKind = true;
      hasThreeOfAKind = true;
    } else if (count[key] === 3) {
      hasThreeOfAKind = true;
    }
  }
  const threeOfAKindIndex = 0; 
  const fourOfAKindIndex = 1; 
  const finalOptionIndex = 2; 
  if (hasFourOfAKind) {
    updateRadioOption(fourOfAKindIndex, totalScore);
    updateRadioOption(threeOfAKindIndex, totalScore);
  } else if (hasThreeOfAKind) {
    updateRadioOption(threeOfAKindIndex, totalScore);
  } else {   
    updateRadioOption(finalOptionIndex, 0);
  }
};
rollDiceBtn.addEventListener("click", () => {
  if (rolls === 3) {
    alert("You have made three rolls this round. Please select a score.");
  } else {
    rolls++;
    rollDice();  
    updateStats();
    getHighestDuplicates(diceValuesArr); 
  }
});

still getting the same error in the console.

Check your index for the finalOptionIndex variable.

There are 6 options, starts at index 0, and the last option is the one for the score of 0

1 Like

Ples stop writing answers for people

1 Like

I don’t quite get what are you trying to say can you be a little more specific, sorry i’m getting a bit frustrated with this .

Managed to solve it Thanks a lot