Skip to content
Advertisement

Frequency of each value in an array of random integers

Need help with an assignment where I have to generate random runs between 1 to 6 for 30 balls and get : 1.Total runs scored 2.Number of 0s, 1s, 2s, 3s, 4s and 6s 3.Strike Rate

While I have got ‘Total runs’ and ‘Strike rate’, I am unable to get the frequency of 0s,1s… I have tried using counter and stream method, but can’t seem to get it right. Your help is highly appreciated. Thank you!

And here’s the actual code, I have marked the frequency part as block for now so that atleast other methods execute…

JavaScript

Advertisement

Answer

JavaScript

etc is comparing a newly generated random number. You want to compare what is already used for the ball:

if ( ball[i] == 0 ) .. etc. although using an array to store the counts instead of individual variables would be cleaner and less code.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement