Although the page seems to be passing the tests, the console says that I still need my Three and Four of a kind options to work. I’m failing steps 4,6 & 7 specifically and not sure how to fix this.
Any help is appreciated!
Your code so far
/* file: script.js */
// User Editable Region
const getHighestDuplicates = (arr) => {
updateRadioOption(5, 0);
const sumOfRoll = diceValuesArr.reduce((a, b) => a + b, 0);
const duplicates = arr.reduce((acc, curr) => {
acc[curr] = (acc[curr] || 0) + 1;
return acc;
}, {})
console.log(duplicates);
for (const [key,value] of Object.entries(duplicates)) {
console.log(`${key}, ${value}`);
if (value >= 4) {
updateRadioOption(1, sumOfRoll);
console.log(sumOfRoll)
} else if (value >= 3) {
updateRadioOption(0, sumOfRoll);
} 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(diceValuesArr);
}
});
// User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) 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
If you declare a variable that will change its value (updating everytime you run a loop for example) - it’s better to use let than const.
Not sure if this was intentional, but your for loop is running the if statements several times, which in turn is updating the radio options over and over again. I recommend setting a variable that keeps track of the maximum count in the for loop then using that value to update the radio options in the if statements outside of the for loop.
I believe I found the reason for this. I was a bit confused by the last requirement that stated: " Regardless of the outcome, the final option should be updated with a score of 0".
I called the updateRadioOption function at the top of the getHighestDuplicates function which I believe was the culprit for that behavior. It’s now been removed and is no longer behaving that way.
Here is my updated code:
const getHighestDuplicates = (arr) => {
let sumOfRoll = diceValuesArr.reduce((a, b) => a + b, 0);
const duplicates = arr.reduce((acc, curr) => {
acc[curr] = (acc[curr] || 0) + 1;
return acc;
}, {})
let max = Math.max(...Object.values(duplicates));
if (max >= 4) {
updateRadioOption(1, sumOfRoll);
} else if (max >= 3) {
updateRadioOption(0, sumOfRoll);
} else {
updateRadioOption(5, 0);
}
}
Thank you for the tips. I’ve switched around my const and let declarations.
As for tip 2, I removed the for loop altogether and took your advice of creating a max count variable and then used that variable for my if statements. It seems that I’m still running into the same issue as described in my original post where steps 4,6, & 7 are failing.
I’m sure it’s something that I’m not grasping correctly but I just can’t figure out what it is.
Here’s my updated code:
const getHighestDuplicates = (arr) => {
let sumOfRoll = diceValuesArr.reduce((a, b) => a + b, 0);
let duplicates = arr.reduce((acc, curr) => {
acc[curr] = (acc[curr] || 0) + 1;
return acc;
}, {})
let max = Math.max(...Object.values(duplicates));
if (max >= 4) {
updateRadioOption(1, sumOfRoll);
} else if (max >= 3) {
updateRadioOption(0, sumOfRoll);
} else {
updateRadioOption(5, 0);
}
}
It took me forever lol but I finally found out what I was doing wrong. The sumOfRoll variable was calculated with diceValuesArr instead of the parameter. I also needed an extra updateRadioOption call in my first if statement for both 3 of a kind and 4 of a kind to be enabled.