-4
I have an array with the height of several students and need to sort in groups by height. I do a for to scan the array and inside the for a conditional if to know if the student enters that group, and then I fill the group array.
The problem is that the array is filling with spaces null and I don’t know if the mistake is in if.
var alunos = [170, 159, 151, 187, 156,]
function qualGrupo(alunos) {
for (var i = 0; i < alunos.length; i++) {
if (alunos[i] >= 150 && alunos[i] <= 159) {
grupoA[i] = alunos[i]
}
}
}
The return is like this: Your groupA array has these values:
[,159,151,,156]
I don’t want you to have these blank spaces. How do you do?
Where does this variable come from
grupoA? You only need to check in with students who are 150 and 159?– Leo Letto
possible duplicate https://answall.com/questions/461226/fun%C3%A7%C3%a3o-javascript-create-arrays-from-another-array
– user60252
that 154 on the return fell from the sky?
– user60252
Use
grupoA.push(alunos[i])instead ofgrupoA[i] = alunos[i]– Rafael Tavares
Leo Letto grupoA was the variable created, and the control would be just that.
– Chrystiomar Chimborski
Leo Caracciolo, had more numbers in the student array I forgot to put. about the duplicate had not found before. Thanks
– Chrystiomar Chimborski
Rafael Tavares will try it. Thank you
– Chrystiomar Chimborski