Tell us what’s happening:
I need help in checking my code, there seems to be a problem with either my getHighestDuplicates function or the arguments passed into it and idk what to do
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/* file: script.js */
// User Editable Region
const listOfAllDice = document.querySelectorAll(".die");
const scoreInputs = document.querySelectorAll("#score-options input");
const scoreSpans = document.querySelectorAll("#score-options span");
const roundElement = document.getElementById("current-round");
const rollsElement = document.getElementById("current-round-rolls");
const totalScoreElement = document.getElementById("total-score");
const scoreHistory = document.getElementById("score-history");
const rollDiceBtn = document.getElementById("roll-dice-btn");
const keepScoreBtn = document.getElementById("keep-score-btn");
const rulesContainer = document.querySelector(".rules-container");
const rulesBtn = document.getElementById("rules-btn");
let diceValuesArr = [];
let isModalShowing = false;
let score = 0;
let round = 1;
let rolls = 0;
// Function to roll dice and update the dice display
const rollDice = () => {
diceValuesArr = [];
for (let i = 0; i < 5; i++) {
const randomDice = Math.floor(Math.random() * 6) + 1;
diceValuesArr.push(randomDice);
}
listOfAllDice.forEach((dice, index) => {
dice.textContent = diceValuesArr[index];
});
};
// Function to update round and roll stats on the UI
const updateStats = () => {
rollsElement.textContent = rolls;
roundElement.textContent = round;
};
// Function to update radio options (score inputs and spans)
const updateRadioOption = (index, score) => {
scoreInputs[index].disabled = false;
scoreInputs[index].value = score;
scoreSpans[index].textContent = `, score = ${score}`;
};
// Function to get the highest duplicate count in the dice array
function getHighestDuplicates(diceArray) {
const frequencyMap = {};
diceArray.forEach(num => {
frequencyMap[num] = (frequencyMap[num] || 0) + 1;
});
return Math.max(...Object.values(frequencyMap));
}
// Event listener for rolling dice
rollDiceBtn.addEventListener("click", () => {
if (rolls === 3) {
alert("You have made three rolls this round. Please select a score.");
return; // Stop further execution if rolls reach 3
}
rolls++; // Increment rolls
rollDice(); // Generate random dice values
updateStats(); // Update round and rolls stats
const highestDuplicateCount = getHighestDuplicates(diceValuesArr);
const totalScore = diceValuesArr.reduce((sum, num) => sum + num, 0);
// Update radio options based on highest duplicate count
if (highestDuplicateCount >= 4) {
updateRadioOption(0, totalScore); // Four of a Kind option
updateRadioOption(1, totalScore); // Three of a Kind option
} else if (highestDuplicateCount === 3) {
updateRadioOption(1, totalScore); // Only update Three of a Kind option
updateRadioOption(0, 0); // Set Four of a Kind option to 0
} else {
updateRadioOption(0, 0); // Set Four of a Kind to 0
updateRadioOption(1, 0);
updateRadioOption( 2, 0); // Final option (index 2)
// Set Three of a Kind to 0
}
// Always update the final option with a score of 0
//updateRadioOption( 2, 0); // Final option (index 2)
});
// Event listener for showing/hiding rules
rulesBtn.addEventListener("click", () => {
isModalShowing = !isModalShowing;
if (isModalShowing) {
rulesBtn.textContent = "Hide rules";
rulesContainer.style.display = "block";
} else {
rulesBtn.textContent = "Show rules";
rulesContainer.style.display = "none";
}
});
// 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