-1
Hello,
I’m doing a very simple counter in javascript, but I’m not getting to do the for appears in the browser, is only appearing the last number of the loop. When I test in the console the result is correct. However, when to show in HTML it does not work. follow the codes
function executar() {
let inicio = document.getElementById('inicio')
let fim = document.getElementById('fim')
let passo = document.getElementById('passo')
let resultado = document.getElementById('resultado')
if( inicio.value.length == 0 || fim.value.length == 0 || passo.value.length == 0){
window.alert('Complete os dados')
} else {
resultado.innerHTML = 'Contando:'
let i = Number(inicio.value)
let f = Number(fim.value)
let p = Number(passo.value)
if (p == 0){
p = 1
}
if ( i < f){
for (let c = i; c <= f; c+=p){
resultado.innerHTML = `${c} \u{1F449}`
}
} else {
for(let c = i; c>= f ; c-= p){
resultado.innerHTML += `${c} \u{1F449}`
}
}
resultado.innerHTML += `\u{1F3C1}`
}
}
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Site 3</title>
<link rel="stylesheet" href="estilo.css">
</head>
<body >
<header>
<h1>Site 3</h1>
</header>
<section>
<div>
<p>Inicio
<input type="number" name="inicio" id="inicio">
</p>
<p>Fim
<input type="number" name="fim" id="fim">
</p>
<p>Passo
<input type="number" name="passo" id="passo">
</p>
<input type="button" value="Executar" onclick="executar()">
</p>
</div>
<div id="resultado">
Preencha os dados...
</div>
</section>
<footer>
<p>© Henrique</p>
</footer>
<script src="script.js"></script>
</body>
</html>
I think I got the question wrong.
– Augusto Vasques
It’s complicated often to understand, but the help of the code in the question was critical to answer @Augustovasques ...
– novic
Show, thanks was only the += that was missing
– Henrique Lima