Review Algorithmic Thinking by Building a Dice Game - Step 7

Tell us what’s happening:

Hi,
I have tried to fine tune this code for a while, but it will not let me pass.

Most of the requirements are not met according to the console. But I am out of ideas.

If someone could point me in the right direction and help me understand, that would be much appreciated.

Thanks

Your code so far

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

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



const getHighestDuplicates = (diceValuesArr) => {
  const counts = {};
  let totalScore = 0;
  
  diceValuesArr.forEach((number) => {
    counts[number] = (counts[number] || 0) + 1;
    totalScore += number;
  });

  const maxCount = Math.max(...Object.values(counts));

  if (maxCount >= 4) {
    updateRadioOption("four-of-a-kind", totalScore);
    updateRadioOption("three-of-a-kind", totalScore);
  } else if (maxCount >= 3) {
    updateRadioOption("three-of-a-kind", totalScore);
  } else {
    updateRadioOption("none", 0);
  }
};
  
rollDiceBtn.addEventListener("click", () => {
  if (rolls === 3) {
    alert("You have made three rolls this round. Please select a score.");
  } else {
    rolls++;
    rollDice();
    updateStats();
    getHighestDuplicates();
  }
});

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

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 7

Hello, from a search on FCC one issue is using the array name as a param, you should try arr in place of it. Also the two let variables are split up into separate parts with the required logic. const counts = {};
let totalScore = 0; the rest seems ok but some are using numbers in place of strings. 4,3, 5 and 0. Good luck

Hi,

thanks for that.
I dont quite understand what you mean by the split up let variables?

In regards to the numbers instead of strings, is that in regards to the updateRadioOption function?

Go back to the function you created called updateRadioOption. What is the first parameter supposed to be?

Also: start logging things to check your work.

In the updateRadioOption the first parameter is index.

I also get mulitple of these - “[TypeError: Cannot set properties of undefined (setting ‘disabled’)]” and one of these "Uncaught ReferenceError: diceValueArr is not defined
[ReferenceError: diceValueArr is not defined] "

Does the second one have anything to do, that my diceValueArr is an empty array?

I am just a bit lost at this point.

Do you recall what the word index meant?

It could be two functions,one with the counts var and the other with the letScore at the top of each one, I wouldnt be too concerned about the updateRadioBuntton variable but yes that was it. If you type in the heading to this step 7 or google it you might be supprised at how this one goes because their using a for keyword and a condition like an if statement with some words, something I cant figure out at this point.

From my understanding, index is the “spot” of the number or element in the array.

So index is a number?

Look at your code. Are you indicating a number here?

I see, this is a string, not a number.

I suggest fixing all the calls to update the radio buttons to use an actual index and not a string. I also suggest that you play the game and see if it works as described by the step.

Finally, and this is an issue with the step itself, be sure to update the last radio button regardless of the maxCount (or update that last radio button all the time).

for(const key in numCounts) {
console.log(${key}: ${numCounts[key]})

if (`${numCounts[key]}` <= `2`){
updateRadioOption(5,0)

}else if(${numCounts[key]} === 3) {
updateRadioOption(0,sum(diceValuesArr));

} else {
updateRadioOption(0,sum(diceValuesArr));
updateRadioOption(1,sum(diceValuesArr));
}

what is still to be added for the code to let me pass, cos the coding is workiing as it should while playing the game

You need to update the last radio button all the time (forget about the if statement, just update this radio button every time the function is called)

i still dont get what you are trying to say

Tell me what you understood from my last reply please and if it is wrong i will give you feedback.

what I understood there is that, the update radioButton function Shud b called in each of the if statement conditions, while updating the last radiobutton option or the updateRadionButton(5,0) Shud be included in the getHigherDuplicates function itself

not quite what I meant but thank you for telling me what you understood.
What I was saying was a comment about the last radio button only.
In the step they say:

If neither of those are true, the final option should be updated with a score of 0

But what I’m saying is, ignore this instruction and ALWAYS update the final option with the score of 0.
(do not update it under a specific condition being true or false, just update it every time your new function gets called)

alright, thanks
will keep trying it

Not sure if this is still an issue but one thing i have noticed is you made a function getHighestDuplicates, that function has a placeholder of diceValuesArr.

when you click the rollDiceBtn and call the getHighestDuplicates function you do not put in a parameter to take place of the diceValuesArr placeholder. There is no reference as to what diceValuesArr placeholder should be when the function executes.

i fiddled around with your code and found 3 issues with the original post. until i got the correct answer

  1. you do not reference what the placeholder should be when calling getHighestDuplicate function.

  2. the updateRadioOption takes a number instead of a string in the index. (COUNTING STARTS FROM 0 :wink: )

  3. ON CONDITION that maxCount is LESS THAN or Equal to 2 update the last radio option to have a score of 0. edit, the code passes without a condition so the only issues were the top 2. oops XD