Doubt in JS array logic

Asked

Viewed 1,306 times

0

I need to make the array received from the function HAPPENS (in this function I receive the schedules of students who entered early, late and punctual. ) go to the function OPENINGS (and here I inform if the class happens or not, as shown in the statement below), ie I want to pull the information received in HAPPENS, That’s what the exercise asks for:

Write the following functions: 1. happens, that says if the class will succeed according to the array of incoming students. 2. openings, that uses an array with the arrays of students who entered the others days, and the minimum number of students, and say which days the classes happened and which did not. For example:

aberturas([alunosDaSegunda, alunosDaTerça, alunosDaQuarta], 
[true, false, false]

This is my code so far, but I don’t know if the logic is correct, I need help to understand.

function acontece(alunos){
  var alunosDaSegunda = []
  var alunosDaTerca = []
  var alunosDaQuarta = []
  for (var i = 0; i < alunos.lenght; i++){
    if(i <= 0){
      return alunos
    }else
      return 
  }
}

function aberturas(temAula){
  var alunosDaSegunda = []
  var alunosDaTerca = []
  var alunosDaQuarta = []
  for(var i = 0; i < temAula.lenght; i++ )
    if(temAula > 2 ){
      return temAula
    }else
      return

}

1 answer

1


You have to understand that when you declare return in a function, its execution is terminated. It is of no use to declare a for and then return in the first interaction. yield has a behavior more similar to what you are trying to do, but there is no need to use it in this case.

Take the example:

function acontece(arrAlunos, qtdMinima) {
    var alunosPontuais = 0;
    for (var aluno of arrAlunos) {
        if (aluno <= 0)
            alunosPontuais++;
    }

    return alunosPontuais >= qtdMinima;
}

function aberturas(arrAlunosDias, qtdMinima){
    var arrDiasComAulas = [];
    for (var alunosDia of arrAlunosDias) {
        arrDiasComAulas.push(acontece(alunosDia, qtdMinima));
    }

    return arrDiasComAulas;
}

// 0 = aluno pontual, -1 = aluno adiantado, 1 = aluno atrasado
var arrDiasComAulas = aberturas(
    [
        [0, 0, 0],   //Dia 1
        [-1, 0, -1], //Dia 2
        [1, 1, 1]    //Dia 3
    ], 2 //Mínimo de alunos para contecer uma aula
);

console.log(arrDiasComAulas);

  • Okay, thank you so much for the explanation!!

Browser other questions tagged

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