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