Array, how to apply in this question?

Asked

Viewed 2,691 times

0

I have the following question:

A programming teacher, tired of students arriving late, decided to cancel class if there are few gifts.

It represents the entrance of students as a array of arrival times late, in minutes.

For example, if a student arrived 10 minutes late, another 5 minutes before the hour, another with 3 minutes late, and another punctual, may thus represent:

var alunosDaSegunda = [10, -5, 3, 0];

With this information and the minimum number of students to succeed the course, the teacher wants to know if the class will take place.

For example, assuming that the minimum number of students for the class to take place is 2 students, then the course of Monday will take place, because there was a student who was punctual and a student who arrived early.

acontece(alunosDaSegunda, 2)
// true

But if the minimum quantity was 3, the class would not happen:

acontece(alunosDaSegunda, 3)
// false

Write the following functions:

  1. acontece, which says whether the class will succeed according to the array of the students who entered.
  2. aberturas, using a array with the arrays of students who entered the other days, and the minimum amount of students, and tell which days the classes took place and which.

    For example:

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

Added as reply - 14/05/2019 at 22:49

Guys, I can’t find the solution.

function acontece(estudantes) {

    var quantidade = estudantes.length;

    var positivos = 0;

    for (var i = 0; i < quantidade; i++) {

        if (estudantes[i] <= 0) {

            positivos = positivos + 1; 

        }
    }
}

How can I get it right ?

  • Welcome to Stackoverflow in English. I edited your question to remove the greetings as we usually keep the text as clean as possible to focus on your scheduling question. If you are interested in visiting a part of the site that is not aimed to ask questions can know the [chat]. If you have questions about the operation, rules and procedures of the site visit the [meta] :)

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

5 answers

2

function acontece(horario, alunos){
  var qtd = 0;  
  for(var i = 0; i < horario.length; i++){
    if(horario[i] < 1){
      qtd++;
    }
  }  
  return qtd >= alunos;
}

function aberturas(dias, alunos){
var qtd = 0;
  var vet = []
  for(var x = 0;x < dias.length; x++){
    for(var i = 0; i < dias[x].length; i++){
      vet[x] = acontece(dias[x], alunos);
  }
    
  }  
  return vet;
}

0

You have to create a function that checks a specific array then creates another one that calls the first one and checks several arrays. So:

    function acontece(arr, num) {
      if(arr.length < num ) {
        return false;
      }

      let presentes = 0;
      for(let i=0;i<arr.length;i++) {
        if(arr[i] <= 0){
          presentes += 1;
        }
      }

      return (presentes >= num);
    }

    function aberturas(arr, num) {
      let arrAcontece = [];
      for(let i=0; i<arr.length;i++){
        arrAcontece.push(acontece(arr[i], num));
      }

      return arrAcontece;
    }

0

As you can only use for and if, below is my solution:

function acontece(estudantes, minimo) {
    var quantidade = 0; //variável contadora
    for(var i = 0; i < estudantes.length; i++) {
        if(estudantes[i] <= 0) { //compara se o estudante [i] chegou na hora
            quantidade++;
        }
    }
    return quantidade >= minimo; //condição booleana
}

function aberturas(dias, minimo) {
    var respostas = []; //array de respostas
    for(var i = 0; i < dias.length; i++) {
        respostas[i] = acontece(dias[i], minimo);
    }
    return respostas;
}

console.log(acontece([10, -5, 3, 0], 2)) //true
console.log(acontece([10, -5, 3, 0], 3)) //false

console.log(aberturas([[10, -5, 3, 0], [10, -5, 3, 0]], 2)) // [true, true]
console.log(aberturas([[10, -5, 3, 0], [10, -5, 3, 0]], 3)) // [false, false]

</code>


0

You will need a variable to count how many students arrived at the correct time. This variable should start at zero.

Then you should use the go command to scroll through the array and use an if to check that element of the less or equal to zero array. If so, you increment your counter variable.

At the end of the program you check if the counter has the value greater than or equal to 2.

I won’t write code here because that’s your job... ;)

  • I’m trying to ! Let’s see if I can !

0

You can use the filter function:

filter

The method filter() creates a new array with all the elements that passed the test implemented by the provided function.

This function executes another one for each item, which will return true or false. This return will determine whether the item will be in a array resulting. In your case you should check if the item is less than zero and compare the array which will be formed with the minimum value of persons:

const acontece = (tempos, minimo) => tempos.filter((tempo) => tempo <= 0).length >= minimo;
const aberturas = (dias, minimo) => dias.map((dia) => acontece(dia, minimo));

console.log(acontece([10, -5, 3, 0], 2));
console.log(acontece([10, -5, 3, 0], 2));
console.log(aberturas([[10, -5, 3, 0], [10, -5, 3, 1], [10, 1, 3, 0]], 2));

For the function aberturas you only need to perform the function described above (acontece). The most effective method to accomplish this by returning a array is by function map.


map

The method map() invokes the function callback passed by argument to each Array element and returns a new Array as a result.

  • Thanks for the answer, but I can only use for -> if and Function, I’m trying to figure out the basis you gave me !

Browser other questions tagged

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