I wrote this code to display a table of random numbers between two values using vanilla JavaScript, I used an array to store the random values, and then I displayed them on the page using tales. and I want to count the number of occurrences of every number in this table and display that number. I tried to use a for loop but it didn't work. Can anybody help?
//Variables Exercice 3
var dim = document.getElementById("dimensions");
var min = document.getElementById("min");
var max = document.getElementById("max");
var table = document.getElementById("table");
var btn = document.getElementById("btn");
//Function Exercice 3
btn.addEventListener("click", generate);
function generate() {
if (dim.value == "" || min.value == "" || max.value == "") {
alert("Merci de remplir tous les champs");
} else if (
Number(min.value) > Number(max.value) ||
Number(min.value) == Number(max.value)
) {
alert("La valeur minimale doit être inférieur à la valeur maximale");
} else {
for (var j = 0; j < Math.floor(dim.value / 10); j++) {
var row = document.createElement("tr");
for (var i = 0; i < 10; i++) {
var arr = [];
var cell = document.createElement("td");
while (arr.length < dim.value) {
var r = Math.floor(
Math.random() * (Number(max.value) - Number(min.value)) +
Number(min.value)
);
arr.push(r);
}
table.append(row);
row.append(cell);
cell.innerHTML = arr[i];
}
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Examen 4 JavaScript</title>
</head>
<body>
<div class="Ex1">
<form>
<h1>Exercice 3</h1>
<div class="container">
<div class="dimensions">
<label for="dimensions">Dimension du tableau</label>
<input type="text" name="dimensions" id="dimensions" pattern="[0-9]+" title="Merci de fournir que des numéros" />
</div>
<div class="min">
<label for="min">La valeur minimale</label>
<input type="text" name="min" id="min" pattern="[0-9]+" title="Merci de fournir que des numéros" />
</div>
<div class="max">
<label for="max">La valeur maximale</label>
<input type="text" name="max" id="max" pattern="[0-9]+" title="Merci de fournir que des numéros" />
</div>
</div>
<button type="button" id="btn">Générez un tableau</button>
<table id="table"></table>
</form>
</div>
<script src="main.js"></script>
</body>
</html>