-1
Look, guys, all right? I created a function that prints the prime numbers according to the value entered by the user in the input. I’m having a hard time accessing this number so that the function works and returns an array and imrprir this array in a dynamic table. I’m still starting the study, someone can give me a hand?
Follows the code:
<div class="card border-info container my-4 p-5 col-sm-4">
<form class="text-center p-5" action="#!">
<p class="h4 mb-4">Projeto - Números Primos</p>
<div class="col">
<input type="text" id="numerocalculo" class="form-control" name="numerocalculo" placeholder="Insira um número aqui" onblur="desafioPrimos(this);">
<input class="btn btn-outline-primary waves-effect btn-sm my-4 btn-block" type="button" value="Enviar" onclick="geradorTabela()">
</div>
</form>
</div>
<div class="card border-info mb-3 container p-5 text-center col-sm-4" id="divTabela"></div>
<script type="text/javascript">
/* FUNÇÃO NÚMEROS PRIMOS */
var numero = document.getElementById('numerocalculo').value;
var primos = [];
function desafioPrimos() {
numero = parseInt(numero);
numerosPrimos:
for (var x = 2; x <= numero; x++) {
for (var y = 2; y < x; y++) {
if (x % y === 0)
continue numerosPrimos;
};
primos.push(x);
};
};
/* FUNÇÃO GERADOR DE TABELA */
function geradorTabela() {
var primos = new Array();
primos.push([desafioPrimos()]);
var table = document.createElement("TABLE");
table.border = "0";
var columnCount = primos[0].length;
var row = table.insertRow(-1);
for (var i = 0; i < columnCount; i++) {
var headerCell = document.createElement("TH");
headerCell.innerHTML = primos[0][i];
row.appendChild(headerCell);
}
//Dados.
for (var i = 1; i < primos.length; i++) {
row = table.insertRow(-1);
for (var j = 0; j < columnCount; j++) {
var cell = row.insertCell(-1);
cell.innerHTML = primos[i][j];
}
}
var divTabela = document.getElementById("divTabela");
divTabela.innerHTML = "";
divTabela.appendChild(table);
}
</script>
Thanks, I managed to return the array, I inserted parseint into the function as suggested!
– Millena Oliveira