How to add variables correctly?

Asked

Viewed 339 times

1

I have a series of checkboxes where the user must select several options and each option increments one or more variables.

// Variáveis das características citopáticas 

var hipercromasia = document.getElementById("hipercromasia");
var cromatina_grosseira = document.getElementById("cromatina_grosseira");
var queratinizacao = document.getElementById("queratinizacao");
var cariomegalia = document.getElementById("cariomegalia");
var binucleacao = document.getElementById("binucleacao");
var coilocito = document.getElementById("coilocito");

// Variáveis que armazenam os checkboxes selecionados

var ASCUS = [];
var ASCH = [];
var LSIL = [];
var HSIL = [];

// Verifica os critérios citológicos

if (hipercromasia.checked) {
  ASCUS++;
  ASCH++;  
} 
if (cromatina_grosseira.checked) {
  HSIL++;
}
if (queratinizacao.checked) {
  LSIL++;
  HSIL++;   
}
if (cariomegalia.checked) {
  HSIL++;
  ASCUS++;  
}
if (binucleacao.checked) {
  HSIL++;
  ASCH++; 
}
 if (coilocito.checked) {
   ASCUS++;
   LSIL++;   
} 

 alert(ASCUS + ASCH + LSIL + HSIL);

The problem occurs when I try to add the result of the variables to later make the percentage and etc.

For example: If I select "Hyperchromasia" and "Coarse chromatin", it returns me 21, not 3.

How should I sum up these variables?

  • 1

    Why are your variables vectors initially? Why not 0?

1 answer

3


You must start these counters with 0 and not with [].
There is also a lack of logic to decrease these values if a checkbox is unchecked. If you have a function that runs whenever you need it, this logic can be omitted.

A suggestion would be to do so:

// Variáveis das características citopáticas 
var hipercromasia = document.getElementById("hipercromasia");
var cromatina_grosseira = document.getElementById("cromatina_grosseira");
var queratinizacao = document.getElementById("queratinizacao");
var cariomegalia = document.getElementById("cariomegalia");
var binucleacao = document.getElementById("binucleacao");
var coilocito = document.getElementById("coilocito");

function somar() {
  // Variáveis que armazenam os checkboxes selecionados
  var ASCUS = 0;
  var ASCH = 0;
  var LSIL = 0;
  var HSIL = 0;

  // Verifica os critérios citológicos
  if (hipercromasia.checked) {
    ASCUS++;
    ASCH++;
  }
  if (cromatina_grosseira.checked) {
    HSIL++;
  }
  if (queratinizacao.checked) {
    LSIL++;
    HSIL++;
  }
  if (cariomegalia.checked) {
    HSIL++;
    ASCUS++;
  }
  if (binucleacao.checked) {
    HSIL++;
    ASCH++;
  }
  if (coilocito.checked) {
    ASCUS++;
    LSIL++;
  }
  return ASCUS + ASCH + LSIL + HSIL;
}

var soma = somar();
console.log(soma);
  • 1

    It worked perfectly, it was exactly what I needed, thank you very much!

  • Dear, Math.abs() returns 0 when the value is null and returns a number when the value is a string ([MDN[(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/abs)). So my answer was valid. I just activate myself in the scope of the problem posed by the author of the question, in which he informs that the problem was "instead of adding up 2+1 = 3, it was returning 2+1 = 21". From the content of the question, I understood that this was the only doubt, and at no time did he make it clear that he needed a complete code.

  • ...His answer, although it solved the problem (as well as mine), did not please me for escaping, in my view, from the scope of the question. It’s like the person says, "I can’t open my car with this key," and you’ve provided a complete car for the person.

  • @Dvdsamm thanks for clarifying! In my view the problem is to be declaring counters that start at zero and go adding with []. That’s the problem with logic and my only change to the AP code. Hence my first line Deves iniciar esses contadores com 0 e não com [].. What I did afterwards was to encapsulate the code with the correction described above in a function. I didn’t change anything else.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.