Review Algorithmic Thinking by Building a Dice Game - Step 13

Tell us what’s happening:

My test is failing, despite my application working properly both when I manually set my object to a full house (see commented out line in the code), and after testing the program enough times to finally roll one. I’m suspecting this is because of how the test is set up, that I fail because I moved the declaration of my count object to the global space, and re-used the work from the getHighestDuplicates method. I would appreciate feedback on this approach, even though I will try re-doing steps to see if I can pass the test.

Your code so far

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

/* file: styles.css */

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

const detectFullHouse = () => {
//counts = {1: 1, 2: 1, 3: 3}
  if(Object.keys(counts).length === 2 && Object.values(counts).includes(3)) {
    updateRadioOption(2, 25);
  } else { 
    updateRadioOption(5, 0);
  }
}

const resetRadioOptions = () => {
  scoreInputs.forEach((input) => {
    input.disabled = true;
    input.checked = false;
  });

  scoreSpans.forEach((span) => {
    span.textContent = "";
  });
};

const resetGame = () => {
  diceValuesArr = [0, 0, 0, 0, 0];
  score = 0;
  round = 1;
  rolls = 0;

  listOfAllDice.forEach((dice, index) => {
    dice.textContent = diceValuesArr[index];
  });

  totalScore.textContent = score;
  scoreHistory.innerHTML = "";

  currentRoundRolls.textContent = rolls;
  currentRound.textContent = round;

  resetRadioOptions();
};

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

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

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 13

I think I see a logic problem in your code. Let’s say I have a five dice and wanna know if I ended up with a full house. A full house being that there are a pair and a three of a kind present. How do you plan on determining what was rolled without looking at the dice?

Thanks for this reply - I did manage to pass the test by simply repeating the steps from my getHighestDuplicates method of re-creating the ‘counts’ object. I had initially declared it in the global space, so that I could re-use the work across methods, but this didn’t satisfy the test.

It raises a more general question for me, which is whether there is a preference to repeat code so that each method/function stands alone in case one is subsequently removed vs the performance hit of re-running the same task multiple times. Beyond this, I’m wondering why not simply check for all scores in one method rather than separating out the work. I imagine it’s because the code is easier to read and learn, but is there any reason to do this once more experienced with coding?

you don’t want to repeat code in multiple places.
That just increases the work you have to do to fix it later or refactor it.
The code in the project may not be as well designed as it could be because the goal is to teach coding more than to write perfect code.

There might be a way of not repeating the code. Is it possible to check if scoreInputs[0] is enabled? This would be checking if there is a three of kind. If the code doesn’t pass on this basis, I would wonder if the code evaluation is only too subjective.