Review Algorithmic Thinking by Building a Dice Game - Step 7

Tell us what’s happening:

My code seems to be updating the radio buttons for the 3 of a kind, 4 of a kind use cases in the preview. However the code does not pass. I am not sure what the issue is here. Can someone guide me?

Your code so far

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

/* file: styles.css */

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

const getHighestDuplicates = (arr) => {
  var highestDuplicate = 0;
  const duplicates = {};
  for(var num of arr){
    duplicates[num] = (duplicates[num] || 0) + 1;
    highestDuplicate = highestDuplicate < duplicates[num] ? duplicates[num] : highestDuplicate;
  }
  if(highestDuplicate === 4){
      score = arraySum(diceValuesArr);
      updateRadioOption(1, score);
      updateRadioOption(0, score);
    } 
    else if(highestDuplicate === 3){
      score = arraySum(diceValuesArr);
      updateRadioOption(0, score);
    } 
    else {
      score = 0;
      updateRadioOption(5, score)
    }
}

const arraySum = (arr) => arr.reduce((accumulator, cuVal) => accumulator + cuVal,0);

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

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 7

Hello, I checked your code on Pytutor.com not much showing up in the way of errors. From reading and looking at other examples this part of the var highestDuplicate = 0; const duplicates = {}; is split with its own logic. both with for (key in object) { // code block to be executed } with the second one executing the first. Good luck

Welcome to the forum @wintermute

Try using the array passed to the function.

Happy coding

Thank you for the reply. I am still not able to crack this - I am passing the dice values as arr to the function. I am able to observe that highestDuplicate captures the correct number of highest duplicates (via console.log). Can you please be a little more specific about your feedback?

share your updated code please

const arraySum = (arr) => arr.reduce((accumulator, cuVal) => accumulator + cuVal,0);

const getHighestDuplicates = (arr) => {
  var highestDuplicate = 0;
  const duplicates = {};
  for(var num of arr){
    duplicates[num] = (duplicates[num] || 0) + 1;
    highestDuplicate = highestDuplicate < duplicates[num] ? duplicates[num] : highestDuplicate;
  }
  if(highestDuplicate >= 4){
      score = arraySum(diceValuesArr);
      updateRadioOption(1, score);
      updateRadioOption(0, score);
    } 
    else if(highestDuplicate >= 3){
      score = arraySum(diceValuesArr);
      updateRadioOption(0, score);
    } 
    else {
      score = 0;
      updateRadioOption(5, score)
    }
}



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

there are some places inside your getHighestDuplicates in which you use the global variable diceValuesArr instead of the parameter, you should change that