I can’t seem to pass FCC’s tests on this step, but as far as I can tell my code fulfills the conditions required needed to pass. My code correctly detects when there is a full house and when there isn’t, and updates the radio buttons correctly. I’ve tweaked it to work correctly a couple times but the console message I receive keeps either saying that my code doesn’t detect the full house (even though it does) OR that it is not updating the radio buttons correctly (even though it does). I’ve tested my code out a lot and on my end everything appears to be working correctly. Here is the entirety of my code with the code in question beginning at the detectFullHouse arrow function:
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;
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];
});
};
const updateStats = () => {
rollsElement.textContent = rolls;
roundElement.textContent = round;
};
const updateRadioOption = (index, score) => {
scoreInputs[index].disabled = false;
scoreInputs[index].value = score;
scoreSpans[index].textContent = `, score = ${score}`;
};
const updateScore = (selectedValue, achieved) => {
score += parseInt(selectedValue);
totalScoreElement.textContent = score;
scoreHistory.innerHTML += `<li>${achieved} : ${selectedValue}</li>`;
};
const getHighestDuplicates = (arr) => {
const counts = {};
for (const num of arr) {
if (counts[num]) {
counts[num]++;
} else {
counts[num] = 1;
}
}
let highestCount = 0;
for (const num of arr) {
const count = counts[num];
if (count >= 3 && count > highestCount) {
highestCount = count;
}
if (count >= 4 && count > highestCount) {
highestCount = count;
}
}
const sumOfAllDice = arr.reduce((a, b) => a + b, 0);
if (highestCount >= 4) {
updateRadioOption(1, sumOfAllDice);
}
if (highestCount >= 3) {
updateRadioOption(0, sumOfAllDice);
}
updateRadioOption(5, 0);
};
const detectFullHouse = (arr) => {
const count1 = {};
const count2 = {};
let fullHouse = 0;
for (const num1 of arr) {
if (count1[num1]) {
count1[num1]++;
} else {
count1[num1] = 1;
}
}
for (const num2 of arr) {
if (count2[num2]) {
count2[num2]++;
} else {
count2[num2] = 1;
}
}
for (const num1 of arr) {
const highPair = count1[num1];
if (highPair === 3) {
for (const num2 of arr) {
const lowPair = count2[num2];
if (lowPair === 2 && highPair) {
fullHouse = true;
updateRadioOption(2, 25);
updateRadioOption(5, 0);
console.log(highPair, lowPair);
} else if (!fullHouse) {
return updateRadioOption(5, 0);
};
};
};
};
};
const resetRadioOptions = () => {
scoreInputs.forEach((input) => {
input.disabled = true;
input.checked = false;
});
scoreSpans.forEach((span) => {
span.textContent = "";
});
};
const resetGame = () => {
diceValuesArr = [0, 0, 0, 0, 0];
score = 0;
round = 1;
rolls = 0;
listOfAllDice.forEach((dice, index) => {
dice.textContent = diceValuesArr[index];
});
totalScoreElement.textContent = score;
scoreHistory.innerHTML = "";
rollsElement.textContent = rolls;
roundElement.textContent = round;
resetRadioOptions();
};
rollDiceBtn.addEventListener("click", () => {
if (rolls === 3) {
alert("You have made three rolls this round. Please select a score.");
} else {
rolls++;
resetRadioOptions();
rollDice();
updateStats();
getHighestDuplicates(diceValuesArr);
detectFullHouse(diceValuesArr);
}
});
rulesBtn.addEventListener("click", () => {
isModalShowing = !isModalShowing;
if (isModalShowing) {
rulesBtn.textContent = "Hide rules";
rulesContainer.style.display = "block";
} else {
rulesBtn.textContent = "Show rules";
rulesContainer.style.display = "none";
}
});
keepScoreBtn.addEventListener("click", () => {
let selectedValue;
let achieved;
for (const radioButton of scoreInputs) {
if (radioButton.checked) {
selectedValue = radioButton.value;
achieved = radioButton.id;
break;
}
}
if (selectedValue) {
rolls = 0;
round++;
updateStats();
resetRadioOptions();
updateScore(selectedValue, achieved);
if (round > 6) {
setTimeout(() => {
alert(`Game Over! Your total score is ${score}`);
resetGame();
}, 500);
}
} else {
alert("Please select an option or roll the dice");
}
});
I must be missing something big while focusing on the minutia. Any point in the right direction would be helpful, but again, as far as I can tell, it satisfies the conditions required by the step.
OPPS, I posted the wrong version of my code above … that one doesn’t work. Here is the one that works:
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;
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];
});
};
const updateStats = () => {
rollsElement.textContent = rolls;
roundElement.textContent = round;
};
const updateRadioOption = (index, score) => {
scoreInputs[index].disabled = false;
scoreInputs[index].value = score;
scoreSpans[index].textContent = `, score = ${score}`;
};
const updateScore = (selectedValue, achieved) => {
score += parseInt(selectedValue);
totalScoreElement.textContent = score;
scoreHistory.innerHTML += `<li>${achieved} : ${selectedValue}</li>`;
};
const getHighestDuplicates = (arr) => {
const counts = {};
for (const num of arr) {
if (counts[num]) {
counts[num]++;
} else {
counts[num] = 1;
}
}
let highestCount = 0;
for (const num of arr) {
const count = counts[num];
if (count >= 3 && count > highestCount) {
highestCount = count;
}
if (count >= 4 && count > highestCount) {
highestCount = count;
}
}
const sumOfAllDice = arr.reduce((a, b) => a + b, 0);
if (highestCount >= 4) {
updateRadioOption(1, sumOfAllDice);
}
if (highestCount >= 3) {
updateRadioOption(0, sumOfAllDice);
}
updateRadioOption(5, 0);
};
const detectFullHouse = (arr) => {
const count1 = {};
const count2 = {};
for (const num1 of arr) {
if (count1[num1]) {
count1[num1]++;
} else {
count1[num1] = 1;
}
}
for (const num2 of arr) {
if (count2[num2]) {
count2[num2]++;
} else {
count2[num2] = 1;
}
}
for (const num1 of arr) {
const highPair = count1[num1];
if (highPair === 3) {
for (const num2 of arr) {
const lowPair = count2[num2];
if (lowPair === 2 && highPair) {
updateRadioOption(2, 25);
updateRadioOption(5, 0);
console.log(highPair, lowPair);
};
};
};
};
};
const resetRadioOptions = () => {
scoreInputs.forEach((input) => {
input.disabled = true;
input.checked = false;
});
scoreSpans.forEach((span) => {
span.textContent = "";
});
};
const resetGame = () => {
diceValuesArr = [0, 0, 0, 0, 0];
score = 0;
round = 1;
rolls = 0;
listOfAllDice.forEach((dice, index) => {
dice.textContent = diceValuesArr[index];
});
totalScoreElement.textContent = score;
scoreHistory.innerHTML = "";
rollsElement.textContent = rolls;
roundElement.textContent = round;
resetRadioOptions();
};
rollDiceBtn.addEventListener("click", () => {
if (rolls === 3) {
alert("You have made three rolls this round. Please select a score.");
} else {
rolls++;
resetRadioOptions();
rollDice();
updateStats();
getHighestDuplicates(diceValuesArr);
detectFullHouse(diceValuesArr);
}
});
rulesBtn.addEventListener("click", () => {
isModalShowing = !isModalShowing;
if (isModalShowing) {
rulesBtn.textContent = "Hide rules";
rulesContainer.style.display = "block";
} else {
rulesBtn.textContent = "Show rules";
rulesContainer.style.display = "none";
}
});
keepScoreBtn.addEventListener("click", () => {
let selectedValue;
let achieved;
for (const radioButton of scoreInputs) {
if (radioButton.checked) {
selectedValue = radioButton.value;
achieved = radioButton.id;
break;
}
}
if (selectedValue) {
rolls = 0;
round++;
updateStats();
resetRadioOptions();
updateScore(selectedValue, achieved);
if (round > 6) {
setTimeout(() => {
alert(`Game Over! Your total score is ${score}`);
resetGame();
}, 500);
}
} else {
alert("Please select an option or roll the dice");
}
});
Hi there!
Post a link to the challenge step.
Hey there…here is the link to the particular step I am stuck on: Dice Game Step 13.
I’ve also attached some screenshots below to show it is working on my end. (They may be mis-titled as Step 11, but they are from Step 13.
Much appreciation for any help you can provide.
The function detectFullHouse
currently uses two separate objects (count1 and count2) to count dice occurrences, but only one count object is needed.
The function iterates through arr multiple times, leading to unnecessary complexity and inefficiency.
The logic for checking three-of-a-kind and two-of-a-kind is overcomplicated, making it harder to understand and debug.
Use a single count object to store the frequency of each dice value.
After counting occurrences, check if there’s a group of three and a group of two to detect a full house.
If a full house is found, the function can update the relevant options and exit early.
Ok, thanks for the help. I got it to work by simplifying the code. The weird thing is that I tested the new code before I submitted my code and it has errors but still works. In one instance, it ended up not even detecting a 3 of a Kind that was not part of a Full House. In the other instance, it didn’t detect a Full House when there was clearly a pair, and 3 of a Kind. It’s very confusing that this version of my code would pass when it has errors and my original version didn’t pass even though it worked perfectly.
Here’s my new code for reference:
removed
So, thanks for the help. I got through the step but now I’m confused as to why I got through it with faulty code.
It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.
Gotcha. My bad. I will refrain from doing so in the future.
1 Like