The way I go about writing an algorithm for something like this is I take a piece of paper and write down an example of the list of dice that I can get.
After that I work out how I would, as a human, figure out which number is repeated the most.
I write down my steps and make sure they are easy to follow by someone who is about 7 years old.
This usually leads me to something I can turn into code.
So ask yourself: if you had a list like
[5,4,5,4,2,5] how would you know that 5 is repeated? And how would you know how many times it is repeated? How would you know that this number of repetitions is more than 4 is repeated?
Write down a step by step description of how you would do that.
So how would you implement this in code? You have two values to keep track of for each iteration through the array:
The current value
The number of times this value has occured.
There is a data structure that is appropriate for this, that uses keys and values. You want to create a kind of table or object with a key for each number and a value for the number of times it occurs.
I think starting with a for loop that iterates through the array is the easiest way to begin.
my diceValuesArr looks like this after 3 rolls : [ 5, 1, 5, 1, 1 ] [ 2, 3, 3, 4, 4 ] [ 4, 5, 6, 4, 3 ]. I don’t think it would be comfortable to count them as is, no? I’d probably like it to be a big single array instead of this
The button to reset will undo all the code changes you did in the step. After you reset, if you print out the diceValuesArray, it will be exactly one array.
If you add any code after that to modify the array, then it will look different.
If you are still stuck, share the full code including the console.log statements you are using to debug.
This looks like 3 different arrays. 1 array per roll.
Make sure to call your getHighestDuplicates when the dice are rolled.
You need to pass the array to your getHighestDuplicates function, and then loop through it and count how many times each number appears, and store that in a new object where each key is one of the numbers, and the value is the number of times it appears.
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
Ok,so I need a way to know how many duplicates each number in array has. But if first roll has two 4 and second has one 4, it should’ve been 3 in total. And because diceValuesArr is the way it is, I don’t think it could work. Is there a way to fix this?