(Spoiler) Review Algorithmic Thinking by Building a Dice Game - Step 7

Tell us what’s happening:

Can someone explain why we loop through ‘arr’ a second time to store highestCount? Is it not possible to search the object we create at the top for those values? I know it’s only 5 values, but it feels inefficient to iterate through ‘arr’ again.

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

const getHighestDuplicates = arr => {
  const duplicates = {};
  const diceSum = arr.reduce((acl, el) => acl + el, 0);

  for (const num of arr) {
    if (duplicates[num]) {
      duplicates[num]++;
    } else {
      duplicates[num] = 1;
    }
  }

  let highestCount = 0;

  for (const num of arr) {
    const count = duplicates[num];
    if (count >= 3 && count > highestCount) {
      highestCount = count;
    }
    if (count >= 4 && count > highestCount) {
      highestCount = count;
    }
  }

  if (highestCount >= 4) {
    updateRadioOption(1, diceSum);
  }

  if (highestCount >= 3) {
    updateRadioOption(0, diceSum);
  }

  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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 7

This isn’t the only possible solution. There’s lots of ways to accomplish this task.