JS/TP01/EX3/minmax.html

60 lines
1.2 KiB
HTML
Raw Normal View History

2024-02-26 19:17:32 +01:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="http://www.iut-fbleau.fr/css/tacit.css">
</head>
<body class="m-6">
<main class="container">
<h2 class="title is-2">Random Array</h2>
<div id="line"></div>
<h2 class="title is-2">Min/Max</h2>
<p>
Min : <span id="min"></span>
</p>
<p>
Max : <span id="max"></span>
</p>
</main>
<script>
function getRandomArray(size,inf,max)
{
// TODO
let i;
let array = [];
for (i=0; i<size; i++){
let x = Math.floor(Math.random() * (max - inf + 1) + inf);
array[i] = x;
}
return array;
}
function minmax(array)
{
// TODO
if (array.length >= 0){
return array.reduce(function(resultat, nombre) {
return{
'min': Math.min(resultat.min, nombre),
'max': Math.max(resultat.max, nombre)
};
},
{
'min': Number.MAX_SAFE_INTEGER,
'max': Number.MIN_SAFE_INTEGER
});
}
}
let array = getRandomArray(50,-100,100)
let minAndMax = minmax(array)
document.getElementById("line").textContent = array
document.getElementById('min').textContent = minAndMax.min
document.getElementById('max').textContent = minAndMax.max
</script>
</body>
</html>