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.
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.
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.