Review Algorithmic Thinking by Building a Dice Game - Step 7

My code for this step appears to be working correctly, but it doesn’t pass. Is the solution evaluator bugged?

const getHighestDuplicates = nums => {
  for (let i = 0; i < scoreInputs.length; i++) {
    scoreInputs[i].disabled = true;
    scoreSpans[i].textContent = "";
  }
  let repeats = {};
  nums.forEach(num => {
    repeats[num] = (repeats[num] || 0) + 1;
  });
  let highest = 0;
  Object.keys(repeats).forEach(key => {
    if (highest < repeats[key]) {
      highest = repeats[key];
    }
  });
  if (highest === 3) {
    updateRadioOption(0, diceValuesArr.reduce((acc, num) => acc + num, 0));
  } else if (highest >= 4) {
    const sum = diceValuesArr.reduce((acc, el) => acc + el, 0);
    updateRadioOption(0, sum);
    updateRadioOption(1, sum);
  }
  updateRadioOption(5, 0);
  return;
}

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

I have verified the sum is correct and that the Three of a Kind and Four of a Kind options are only active when appropriate. I think it maybe has to do with the forEach on my object keys? I know this isn’t the cleanest, but it should work.

This is my console output:

// running tests
4. 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.
6. When the array has four of the same number, your getHighestDuplicates function should update the Four of a Kind radio option with , score = and the total sum of the dice.
7. When the array has four of the same number, your getHighestDuplicates function should also update the Three of a Kind radio option with , score =  and the total sum of the dice.
// tests completed

I always start my debugging assuming I am wrong. What have to tried to verify your function works as you think it does?

I rolled the dice several times and totaled the dice values when there was Three of a Kind or Four of a Kind. The span values always matched up.

I think I just realized what I did wrong. I was disabling the inputs and clearing the spans before rolling the dice, but that wasn’t called for in the instructions.

Well, removing that reset still didn’t make it work. I really don’t understand what I did wrong.

You need to do some troubleshooting.

Add some console.log statements in your code so you can see the input that the tests are sending to your function, and what the output is.

This will give you a better idea of what’s causing it to fail.

Right now it’s a black box, poke some holes in there so you can see the variables.

1 Like