Review Algorithmic Thinking by Building a Dice Game - Step 7

Tell us what’s happening:

I don’t know how to check how many the same number appeared in an array

Your code so far

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

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

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

// User Editable Region
/* file: styles.css */

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 7

Let’s puzzle this out. You have an array of numbers. You need something to hold onto these values and count how many times that number appears. A way of using the numbers themselves as a key to get the count value. There’s quite a few possibilities to use.

i think it may be a stupid question but do i work with numbers from one round or a single roll? it’s not mentioned in the instructions

Also doesn’t diceValuesArr hold onto it?

Yes diceValuesArr holds the dice values for that single round.

i should use that array to complete the step, right?

That array is an important part of the process. You see,the getHighestDuplicates function needs an array passed into it in a function call so that there is data to work with.

yes, i get it. How can i see how many duplicates are in the array?

The easiest way is to simply count how many of each number is in the array; and store a count of the values you find. Ideally making sure to identify what number goes with count.

Is there a way to do it with forEach, filter or for?

A forEach or a for loop could work as they involve iteration. Using filter wouldn’t be as helpful, as it simplify filters out data based on a condition.

so, i need to know how many duplicate are there for each number?

Yes. In order to get the highest duplicates, you need to know how many duplicates there are.

diceValuesArr.forEach(num => num === num )

num is always equal to num.

That’s also not exactly how I would use a foreach loop. A foreach loop is intended to do something like a for loop as it works with one element at a time.

More info here: Array.prototype.forEach() - JavaScript | MDN

but we need to get the duplicates? so we need to check if a number is the same as the previous

There is an unfortunate problem with that. We need the highest amount of duplicates. The best and simplest way is to count how many times each number occurs.

can i use if with that? i just think that the easiest way to make sure there are 3, 4 or less duplicates is with if statement

i’m still unsure what to do

While you can use if statements inside a for loop, you would need an if statement for every potential number. Like if num is equal to 1 , if num is equal is to 2, if num is equal to 3 and so forth. Then inside the loop increase a counter so you know how many ones you have, how many twos, and so on. Every single number needs its own counter variable.

Then once you have your counts, you need to see which counter has the largest number.