Review Algorithmic Thinking by Building a Dice Game - Step 7

Tell us what’s happening:

I’m getting an error that my three of a kind portion is not correct. The console does not have any errors. I’ve read many other forum posts about this step, researched documentation, and cannot figure out what’s wrong. Why isn’t my code passing?

Your code so far

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

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

const getHighestDuplicates = (array) => {
  const count = array.reduce((acc, curr) => {
    acc[curr] = (acc[curr] || 0) + 1;
  return acc;
  }, {});
  
  
  const rollScore = array.reduce((a, b) => a + b);
  
  if (count >= 4) {
    updateRadioOption(0, rollScore);
    updateRadioOption(5, 0);
    updateRadioOption(1, rollScore);
    return;
  } else if (count >= 3) {
    updateRadioOption(1, rollScore);
    updateRadioOption(5, 0);
    return;
  } else {
    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
/* file: styles.css */

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 7

isn’t count an object?

Ah, that must be what I’m missing. Would I then need to access the object property that stores the occurrences of each element? Or is there a different way I can count the occurrences of each number to ensure the returned value is a number? Apologies if this is obvious, I’m learning!

I am not sure how you want to approach this, so I don’t know what suggestions to give. But if you want to use numbers in an object you need to access the property they are stored in, yes

I’ve modified my code but it’s still not passing the 4 of a kind test. Can you help me understand why not?

const getHighestDuplicates = (array) => {
  let count = {};
  score = array.reduce((a, b) => a + b);
  for (let item of array) {
    count[item] = (count[item] || 0) + 1;
    if (count[item] >= 4) {
    updateRadioOption(1, score);
    return;
  } else if (count[item] === 3) {
    updateRadioOption(0, score);
    return;
  } else {
    updateRadioOption(5, 0);
  }
  };
}

1 Like

Check the syntax of your if/else statement.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if…else

Also, if you have 4 of a kind, don’t you also have 3 of a kind?

I fixed it! It was an issue with the return statements.