0
when I place var n1,N2 and s inside the sum Function, only then the result turns out right! because? I’m a beginner.
/*variaveis dos elelentos html*/
var dr1= document.querySelector('input#txtn1')
var dr2= document.querySelector('input#txtn2')
var botao = document.querySelector('button.so')
var r = document.querySelector('div.res')
/*recebe os valores digitados no input*/
var n1= Number(dr1.value)
var n2 = Number(dr2.value)
/*realizar a soma dos valores do input*/
var s = n1 + n2
/*cria um evento para ser chamado*/
botao.addEventListener("click",somar)
function somar(){
r.innerHTML = s
r.style.color = 'red'
}
Of the commentary
/*recebe os valores digitados no input*/
until the comment/*cria um evento para ser chamado*/
move to first line of functionsoma()
.– Augusto Vasques
It is simple, when you execute the code you store the values of the variables, and if you change they are not updated. If you want to get the updated values you have to put the code in the sum function as @Augustovasques commented. Do a debug and you’ll find it easy ;)
– Ricardo Pontual
Vlw, so it is recommended to put the conversion and operation variables within the function as local variables? and when I should or should not use as a global variable would have an example or some bibliography reference about it?
– Eduardops.code
"it is recommended to place the conversion and operation variables within the function as local variables" is not about being recommended, only that it needs to be local pq needs to take the updated value, but a broader idea is always to use the local variable, the global is the exception, only use if really need
– Ricardo Pontual
The vlw, I really thank you, hug.
– Eduardops.code