Review Algorithmic Thinking by Building a Dice Game - Step 7

Tell us what’s happening:

Hint: When the array has three of the same number, your getHighestDuplicates() function should update the “Three of a Kind” radio option with ", score = " and the total sum of the dice.

Isn’t my if statement already achieving this task?

Side-Note: holdArr doesn’t do anything aside from holding the values already iterated through to check duplicates for.

Your code so far

/* 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;

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 getHighestDuplicates = (arr) => {
  let duplicates = 0;
  const holdArr = [];
  arr.forEach((el) => {
    if (holdArr.includes(el)) {
      duplicates++;
    }
    holdArr.push(el);
  });
  // Problem starts here...
  if (duplicates === 3) {
      updateRadioOption(0, diceValuesArr.reduce((acc, currVal) => acc + currVal, 0)); 
    } else if (duplicates === 4) {
      updateRadioOption(1, diceValuesArr.reduce((acc, currVal) => acc + currVal, 0));
    }
  // ...and ends here
  updateRadioOption(scoreInputs.length - 1, 0);
}

rollDiceBtn.addEventListener("click", () => {
  if (rolls === 3) {
    alert("You have made three rolls this round. Please select a score.");
  } else {
    rolls++;
    rollDice(); 
    updateStats();
    getHighestDuplicates([...listOfAllDice]);
  }
});

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/130.0.0.0 Safari/537.36 OPR/115.0.0.0

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 7

Welcome back to the forum @MCRBLX

Try console logging the duplicates variable.

Happy coding