Review Algorithmic Thinking by Building a Dice Game - Step 7

Tell us what’s happening:

const getHighestDuplicates = (arr) => {
let countObj = {}
let totalScore = 0;
let maxCount = 0;
let maxDuplicate = null;
arr.forEach((num) => {
countObj[num] = (countObj[num] || 0) + 1;
totalScore += num;
});
if (countObj[num] > maxCount) {
maxCount = countObj[num]; // value
maxDuplicate = num // key
}

if (maxCount < 3) {
updateRadioOption(5, 0)
} else if (maxCount === 3) {
updateRadioOption(0, totalScore)
} else if (maxCount === 4) {
updateRad

Your code so far

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

/* file: styles.css */

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

const getHighestDuplicates = (arr) => {
  let countObj = {}
  let totalScore = 0;
  let maxCount = 0;
  let maxDuplicate = null;
  arr.forEach((num) => {
    countObj[num] = (countObj[num] || 0) + 1;
    totalScore += num;
  });
  if (countObj[num] > maxCount) {
    maxCount = countObj[num]; // value
    maxDuplicate = num  // key
  }

  if (maxCount < 3) {
    updateRadioOption(5, 0)
  } else if (maxCount === 3) {
    updateRadioOption(0, totalScore)
  } else if (maxCount === 4) {
    updateRadioOption(1, totalScore)
  }

};


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

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 7

1 Like

Hi did you have any questions about the code or instructions?

they tell me in the console “num” it’s not defind

Sounds like a very good clue. How can you use this?

Can you find in your code where the num variable is set?

in forEach !
should i put if statement inside the forEach ?

Makes sense.

num is defined only within the forEach. Outside of that method it’s not accessible.

2 Likes