Create an array of length 10 with random values from range 1-99. Then count the tens value and show it as consecutive *. Try to be logical in this question and don’t hard-code this part eg. >0 and <10. Think how you can implement the same code for bigger ranges like 1-999 without increasing the code length. For example:
If array = [9,19,28,29,45,50,51,52,86,97] then it shows:
0-9 : *
10-19 : *
20-29 : **
30-39 :
40-49 : *
50-59 : ***
60-69 :
70-79 :
80-89 : *
90-99 : *
<!DOCTYPE html>
<html>
<meta charset='utf-8'>
<head>
<title></title>
</head>
<body>
<h1>Question Number 4 </h1>
<script>
function getRandomInt(min, max) {
return Math.floor(Math.random() * (Math.floor(max) - Math.floor(min))) + Math.floor(min);
}
let data = new Array(10);
for(let i = 0; i < data.length; i++)
data[i] = getRandomInt(1, 100);
data.sort((a, b) => a - b);
let buckets = new Array(10).fill(0);
for(let i = 0; i < data.length; i++) {
let index = Math.floor(data[i] / 10);
buckets[index]++;
}
document.write('data = [' + data + '] <br>');
for(let i = 0; i < buckets.length; i++) {
document.write((i * 10) + '-' + (i * 10 + 9) + ': ' + '*'.repeat(buckets[i]) + '<br>');
}
</script>
</body>
</html>