Estou fazendo alguns exercícios de Javascript e deparei com esse problema, em fazer contagem de valores booleanos. Converti a NodeList em uma Array para tentar contar os valores pelo método for()
, filter()
switch()
, porém, só consigo o retorno do valor 0, e gostaria de que voltasse a quantidade de true
e false
.
<body>
<h1>Height and Sex</h1>
<p>Type the height and sex: </p>
<div>
<input type="text" name="height" class="height" placeholder="Centimeter" minlength="3" maxlength="3">
<input type="radio" name="op1" value="Man" class="sex"><label>Man</label>
<input type="radio" name="op1" value="Woman" class="sex"><label>Woman</label>
<br>
<input type="text" name="height" class="height" placeholder="Centimeter" minlength="3" maxlength="3">
<input type="radio" name="op2" value="Man" class="sex"><label>Man</label>
<input type="radio" name="op2" value="Woman" class="sex"><label>Woman</label>
<br>
<input type="text" name="height" class="height" placeholder="Centimeter" minlength="3" maxlength="3">
<input type="radio" name="op3" value="Man" class="sex"><label>Man</label>
<input type="radio" name="op3" value="Woman" class="sex"><label>Woman</label>
<br>
<input type="text" name="height" class="height" placeholder="Centimeter" minlength="3" maxlength="3">
<input type="radio" name="op4" value="Man" class="sex"><label>Man</label>
<input type="radio" name="op4" value="Woman" class="sex"><label>Woman</label>
<br>
<input type="text" name="height" class="height" placeholder="Centimeter" minlength="3" maxlength="3">
<input type="radio" name="op5" value="Man" class="sex"><label>Man</label>
<input type="radio" name="op5" value="Woman" class="sex"><label>Woman</label>
</div>
<br>
<button id="btn">Submit!</button>
var sx = document.querySelectorAll(".sex");
let sx_h = [sx[0].checked, sx[2].checked, sx[4].checked, sx[6].checked, sx[8].checked];
let sx_m = [sx[1].checked, sx[3].checked, sx[5].checked, sx[7].checked, sx[9].checked];
for(i = 0; sx_h.values == true; i++){
return i
}
console.log(i)