Hi.
I think this step has a small problem.
When I get pass or fail this step, My pc stops for some seconds and it says
Potential infinite loop detected on line 38 (different lines). Tests may be failing because of this on console.
I think your testing system to check whether code is correct or not makes this mistake. Thank you.
And now when I checked it again, Line percentage was wrong too, it said you completed 21% of this course.
Please post your code and a link to the step.
const listOfAllDice = document.querySelectorAll(".die");
const scoreInputs = document.querySelectorAll("#score-options input");
const scoreSpans = document.querySelectorAll("#score-options span");
const currentRound = document.getElementById("current-round");
const currentRoundRolls = document.getElementById("current-round-rolls");
const totalScore = 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 total = 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 = () => {
currentRoundRolls.textContent = rolls;
currentRound.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);
totalScore.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) => {
let count = {};
arr.forEach(number => {
count[number] = (count[number] || 0) + 1;
})
if(Object.keys(count).length === 2) {
updateRadioOption(2, 25)
} else {
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];
});
totalScore.textContent = score;
scoreHistory.innerHTML = "";
currentRoundRolls.textContent = rolls;
currentRound.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");
}
});