Review Algorithmic Thinking by Building a Dice Game - Step 7

Tell us what’s happening:

const getHighestDuplicates = (diceArray) => {
  const count = {};

  
  diceArray.forEach(num => {
    count[num] = (count[num] || 0) + 1;
  });

  let sum = diceArray.reduce((acc, num) => acc + num, 0); 

  
  let threeOfAKindUpdated = false;
  let fourOfAKindUpdated = false;

  for (let num in count) {
    if (count[num] >= 4 && !fourOfAKindUpdated) {
      
      updateRadioOption(0, sum);  
      fourOfAKindUpdated = true;  
    } else if (count[num] >= 3 && !threeOfAKindUpdated) {

Your code so far

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

/* file: styles.css */

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


const getHighestDuplicates = (diceArray) => {
  const count = {};

  
  diceArray.forEach(num => {
    count[num] = (count[num] || 0) + 1;
  });

  let sum = diceArray.reduce((acc, num) => acc + num, 0); 

  
  let threeOfAKindUpdated = false;
  let fourOfAKindUpdated = false;

  for (let num in count) {
    if (count[num] >= 4 && !fourOfAKindUpdated) {
      
      updateRadioOption(0, sum);  
      fourOfAKindUpdated = true;  
    } else if (count[num] >= 3 && !threeOfAKindUpdated) {
      
      updateRadioOption(1, sum);  
      threeOfAKindUpdated = true; 
    }
  }

  
  updateRadioOption(2, 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);  
  }
});


// 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/131.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 7

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!


I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Hi I need help it’s my first time using this forum

hi @May828

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge. This button only appears if you have tried to submit an answer at least three times.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

Here’s Step 7 Challenge.

If you roll the dice and get a Three of a kind or Four of a kind, then you can get a score totalling the sum of all five dice values. To calculate this, create a getHighestDuplicates function which takes an array of numbers. The function will need to count how many times each number is found in the array.

If a number appears four or more times, you will need to update the Four of a Kind option with your updateRadioOption function. If a number appears three or more times, you will need to update the Three of a Kind option. In both cases, the score value should be the sum of all five dice.

Regardless of the outcome, the final option should be updated with a score of 0. Make sure to call your getHighestDuplicates when the dice are rolled.

And here’s the code with the input checking for Three of a Kind and Four of a Kind, if neither is found (!threeOfAKindUpdated && !fourOfAKindUpdated), the final radio option (index 2) is explicitly updated with a score of 0 using updateRadioOption(2, 0).

// Function to calculate and update Three of a Kind or Four of a Kind
const getHighestDuplicates = (diceArray) => {
  const count = {};

  // Count occurrences of each number in the dice array
  diceArray.forEach(num => {
    count[num] = (count[num] || 0) + 1;
  });

  // Calculate the sum of all five dice
  let sum = diceArray.reduce((acc, num) => acc + num, 0);

  let threeOfAKindUpdated = false;
  let fourOfAKindUpdated = false;

  // Check for Three of a Kind or Four of a Kind
  for (let num in count) {
    if (count[num] >= 4 && !fourOfAKindUpdated) {
      // Update Four of a Kind radio option (index 0)
      updateRadioOption(0, sum);
      fourOfAKindUpdated = true;
    } else if (count[num] >= 3 && !threeOfAKindUpdated) {
      // Update Three of a Kind radio option (index 1)
      updateRadioOption(1, sum);
      threeOfAKindUpdated = true;
    }
  }

  // If no Three of a Kind or Four of a Kind, update the final radio option (index 2) with score = 0
  if (!threeOfAKindUpdated && !fourOfAKindUpdated) {
    updateRadioOption(2, 0);  // Update the final radio option with score = 0
  }
};

// Event listener for rolling dice and updating stats
rollDiceBtn.addEventListener("click", () => {
  if (rolls === 3) {
    alert("You have made three rolls this round. Please select a score.");
  } else {
    rolls++;
    rollDice();
    updateStats();
    getHighestDuplicates(diceValuesArr); // Call getHighestDuplicates to check for Three of a Kind or Four of a Kind
  }
});

But I still receive this error message:
Sorry, your code does not pass. Try again.

When the array has less than three of the same number, your getHighestDuplicates function should update the final radio option with , score = 0.

you have copied the description, not described in your own words what’s happening

in your own words, what do you need help with? what part of the step is giving you issues?

what if it’s a general question about the certification or any question that pops into my mind regarding the process?

open your own topic to ask your own questions

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.