0
I made a simple code for a table using "for" only when customizing for the result of the table to be printed in the DIV I can’t. It does not load all values. Follow the code.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript</title>
<script>
function calcular(){
let valor = document.querySelector('input#valor') // Pega valor digitado no input
let res = document.querySelector('div#resultado') // Pega div (onde deve inserir resultado)
let vfinal = valor.value // Pega o valor pra poder fazer a operação matemática
for(let y = 1; y <= 10; y++) {
let multiplicacao = (vfinal * y)
//document.write( y + ' x ' +valor.value+ ' = ' + (y * valor.value) + '<br>')
res.innerHTML = `${vfinal} x ${y} = ${multiplicacao} ` //
}
}
</script>
<style>
div {
border: 1px solid red;
width: 250px;
height: 350px;
}
</style>
</head>
<h1>Tabuada usando For</h1>
<p><input type="number" id="valor">
<input type="button" value="Calcular" onclick="calcular()"></p>
<p>
<div id="resultado">
Resultado aqui
</div>
</p>
<body>
</body>
</html>
I would like the result to come out like this, according to image:
The
res.innerHTML
assigns a new content to the elementres
, so you’re not getting it. You can use the concatenation (using +sign) to get the result, just addres.innerHTML = ''
(before the is) andres.innerHTML += \
${vfinal} x ${y} = ${multiplication}`` (inside the for)– Valdeir Psr