JSFiddle - React, Tailwind, and code Playground
HTML
Escrever números na caixa de texto e carregar inserir.
Quando tiverem sido inseridos 6 números, sao gerados 4 números aleatórios entre 5 e 20 e mostradas as estatísticas.<br/><br/>
<span>Temperatura:</span><input id="temperature" type="text" placeholder="Escreva temperatura"></input>
<button id="insert">Inserir</button><button id="clear">Limpar</button><span id="error" style="color:red"></span>
<br/><br/>
Array: [<span id="array"></span>]
<br/><br/>
<span id="results"></span>
JavaScript
var temps = [];
$("#insert").on("click", function(){
$("#error").text("");
if(temps.length < 6){
if(!isNaN($("#temperature").val())){
temps.push(parseFloat($("#temperature").val()).toFixed(2));
}
else{
$("#error").text("Número inválido!");
}
}
else{
if(temps.length != 10){
for(i = 0; i < 4; i++){
temps.push(parseFloat((Math.random() * 15) + 5).toFixed(2));
}
}
var max = Math.max.apply(null, temps);
var min = Math.min.apply(null, temps);
var sum = temps.reduce(function(a, b) { return parseFloat(a) + parseFloat(b) });
var avg = sum / temps.length;
$("#results").text("Max: " + max + " Min: "+min + " Sum: "+sum+" Avg: "+avg);
}
$("#array").text(temps.toString());
});
$("#clear").on("click", function(){
temps = [];
$("#array").text("");
$("#results").text("");
});