0
Would you be so kind as to help me?
I’m trying to make a function that adds up several inputs, but instead of adding it is concatenating.
HMTL
<form class="nacional">
<input class="soma-nacional" value="5">
<input class="soma-nacional" value="3">
<input class="soma-nacional" value="2">
</form>
JS
var nacional = document.querySelectorAll(".soma-nacional");
var somaNacional = [];
function somatoriaNacional(){
var soma = [];
for(var i = 0; i < nacional.length; i++){
soma += parseInt(nacional[i].value);
}
console.log(soma); }
Why did you initialize
soma
with an array? It should not have initialized with 0?– Andre
Also, where did the variable come from
nacional
?– Luiz Felipe