Unexpected value when averaging between two numbers [duplicate]

-1
var alunos = document.querySelectorAll(".aluno");

for (var registro = 0; registro<alunos.length; registro++) {

  var aluno= alunos[registro];

  var nota1 = aluno.querySelector(".info-nota1").textContent;

  var nota2 = aluno.querySelector(".info-nota2").textContent;

  var tdMedia = aluno.querySelector(".info-media");
  var media = (nota1+nota2)/2;

  aluno.querySelector(".info-media").textContent=media;

}

    
asked by anonymous 03.08.2018 / 01:13

1 answer

0

When you make note1 + nota2, you are concatenating these variables, rather than adding them together. To solve, just use parseint()

var alunos = document.querySelectorAll(".aluno");
for (var registro = 0; registro<alunos.length; registro++) {
  var aluno= alunos[registro];    
  var nota1 = aluno.querySelector(".info-nota1").textContent;    
  var nota2 = aluno.querySelector(".info-nota2").textContent;    
  var tdMedia = aluno.querySelector(".info-media");
  nota1 = parseInt(nota1);
  nota2 = parseInt(nota2);
  var media = (nota1+nota2)/2;    
  aluno.querySelector(".info-media").textContent=media;    
}
    
03.08.2018 / 01:17