Consultation exercise

0

Good evening, I have a javascript exercise to do and since yesterday I am trying to solve part 2. Let's see if they see where I'm making mistakes.

The enunciation, function openings, asks me to go through an array within another array ([[0, 0, 3, 0], [1, 2, 4, 5], [0, 0, -1]] , 3). Each number in the array is equivalent to the minutes that the students arrived to class. If the same number of students or more arrived with time to spare (it would be the negative numbers or 0) the class opened (True) but not open (false).

I have to access an array of arrays and return the results in another array of results that I create by push. For example: openings ([[0, 0, 3, 0], [1, 2, 4, 5], [0, 0, -1]], 3), you would have to return results [true, false, true]. I do not know if it was clear with the explanation.

Thank you very much!

A programming teacher, tired of the students arriving late, decided that she will cancel the class if there are few present.

She represents the income of the students as an array of late arrival times, in minutes. For example, if a student arrived 10 minutes late, another 5 minutes before the hour, another 3 minutes late, and another one punctual, it represents it like this:

var Revenue on Monday = [10, -5, 3, 0]; With this information and the minimum number of students to open the course, the teacher wants to know if the class opens. For example, assuming that the minimum number of students for the class is 2, Monday's course opens, because there was a student who arrived on time and a student who arrived early.

open (Revenue on Monday, 2) true But if the minimum quantity was 3, the class would not open:

open (Revenue on Monday, 3) false I wrote the following functions:

seOpen, which says if a given class is made the income array openings, which takes an array with the income arrays of several days, and the minimum amount of student, and say what days were opened and what days were not. For example: openings ([RevenueDays, RevenueDays, RevenueDaysight], 2) [true, false, false]

  function aperturas(array, alumnos) {
    var resultado = [];
    var alumnosLlegada = 0;
    for (var i = 0; i < array.length; i++) {
      for (var j = 0; j < array[i].length; j++) {
        if (array[i][j] <=0) {
        alumnosLlegada++
        }
      }
      if (alumnosLlegada >= alumnos) {
        resultado.push(true)
      }else {
        resultado.push(false)
      }
    }
    return resultado
  }
    
asked by Fernando Colom 21.07.2017 в 22:46
source

1 answer

2

Your script works correctly, your only problem is where you declare your variable:

function aperturas(array, alumnos) {
    var resultado = [];
    //antes estaba aqui y  se quedaba con el valor de la ultima iteracion
    for (var i = 0; i < array.length; i++) {
        //debes inicializar la variable cada vez que cambiar de array
        var alumnosLlegada = 0;
        for (var j = 0; j < array[i].length; j++) {
            if (array[i][j] <=0) {
                alumnosLlegada++
            }
        }
        if (alumnosLlegada >= alumnos) {
            resultado.push(true)
        }else {
            resultado.push(false)
        }
    }
    return resultado;
}
    
answered by 22.07.2017 в 00:55