Review Algorithmic Thinking by Building a Dice Game - Step 7

Tell us what’s happening:

What are y’all asking for , exactly? Is this not the way you do it?!

It’s saying “Uncaught TypeError: Cannot read properties of undefined (reading ‘reduce’).” Are we not supposed to reduce? What do we do, then? What do y’all mean, “undefined?” Everything is defined.

Your code so far

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

/* file: styles.css */

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

const getHighestDuplicates = (arr) => {
  const counts = {};
  let total = 0;

  total = arr.reduce((acc, num) => acc + num, 0);

  arr.forEach((number) => {
    counts[number] = (counts[number] || 0) + 1;
  });

  for (let number in counts) {
    if (counts[number] >= 4) {
      updateRadioOption(0, total);
      scoreSpans[0].textContent = `, score = ${total}`;
    } else if (counts[number] >= 3) {
      updateRadioOption(1, total);
      scoreSpans[1].textContent = `, score = ${total}`;
    } else {
      updateRadioOption(5, total);
      scoreSpans[5].textContent = ", score = 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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) 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 7

  1. Did you get the max_frequency first?

scoreSpans[0].textContent = , score = ${total};

  1. This is unnecessary line, because when u call function updateRadioOption(), it’s shall have done this for you.

updateRadioOption(5, total);

  1. Does this line meet what the question ask?

The exercise is asking us to update the text content, though. " When the array has less than three of the same number, your getHighestDuplicates function should update the final radio option with ‘, score = 0.’

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."

check line 38 → updateRadioOption

I know what the function means. I still don’t get it.

I asked ChatGPT, but it’s saying to type out a bunch of new variables, which I don’t feel like doing :roll_eyes: Call me lazy, but it’s late and I’m just trying to get this done.

Step 7 is not easy.

If you get [4,4,4,4,1] , what u should do ?
If you get [4,4,4,2,3], what u should do ?

You can self-test your function by manually modify the arr like this
arr = [4,4,4,1,1]
or
arr = [4,4,4,4,1]

Sorry, I’m still confused. I don’t see how this is helping me.

There is something incorrect of your for-loop. It doesn’t meet algo requirement.
If i would try again, i would start with simple pseudo code:

Example : dice arr = [5, 5, 5 ,5 ,1]
// 5 appears 4 times

  1. Get the max frequency of duplicate => which is 4
  2. Since 5 appears more than 4 times, i update “Four of a Kind” option.
  3. Wait a minute, 5 also appears more than 3 times, i need to update “Three of a Kind” too.

I tried to look it up…


const getHighestDuplicates = (arr) => {
  const counts = {};

  for (i = 0; i < arr.length; i++) {
    if (counts[number]) {
      counts[number]++;
    } else {
      counts[number] = 1;
    }
  }

  let maxCount = 0;

  for (i = 0; i < arr.length; i++) {
    const count = counts[number];
    if (count >= 3 && count > maxCount) {
      maxCount = count;
    }
    if (count >=4 && count > maxCount) {
      maxCount = count;
    }
  }

  const total = diceValuesArr.reduce((acc, num) => acc + num, 0);

  if (maxCount >= 3) {
    updateRadioOption(0, total);
  } else if (maxCount >= 4) {
    updateRadioOption(1, total);
  } else {
    updateRadioOption(5, 0);
  }
}

But nothing works.

I feel like just giving up on the JS certification. Looking it up didn’t help, AI didn’t help, and the forum isn’t helping. Everything’s too vague.

image

Did you checked what your console says?

Yes, That’s also what I don’t get. I did the for loop right. i was defined.

I kept working with it and it kept giving me some error about “Symbol.iterator” not being defined, so I changed some things around and somehow the code passed with the errors still there. :confused: I honestly don’t know what happened and I don’t care. I’m tired.

Being impatient can only help you so much with learning to code. For eg it can help drive you to search for answers faster. This can be detrimental though when one is new to the subject they are trying to learn.
Try to break down the text in the instructions into smaller sentences and even smaller phrases to analyze it before trying to code the solution. It also helps to ask a specific question on the forum. Like maybe you’ve read the text and think you know what it wants but you want to confirm your understanding first and that is great to do (in an interview, when you are given a problem to code , they would expect you to clarify the question with them verbally).
So you can come here and tell us what you believe the instructions are saying and get confirmation first and maybe that will lead to less frustration overall.

If you don’t understand the solution that worked in this step, you can also post it here between two spoiler tags and discuss it with us. Discuss the errors too. Try to take your time and resolve the concepts to your own satisfaction.

(Taking breaks is good too because learning to code is hard).

1 Like

Thanks. I finally got some rest and I’m feeling better. :sweat_smile:

1 Like

My issue is, even though I call the function, I’m getting errors concerning that function.

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.

Which is just plain weird.

if(fourOfAKind){
updateRadioOption(1,sum);
}else if(threeOfAKind){
updateRadioOption(0, sum);
}

This is where I call the updateRadioOption. I’ve triple checked my calculation of the sum too, so that isn’t the issue.

hi there, welcome to the forum.
You’ve posted to someone else’s topic. Please open your own by clicking the Help button to do so (the Help button appears if you’ve unsuccessfully checked your code 3 times).
thanks